Removes spaces (implementation code) between the left and right sides of the string and between the left and right sides of the string.

Source: Internet
Author: User

Removes spaces (implementation code) between the left and right sides of the string and between the left and right sides of the string.
In daily work, it is common to filter some special characters in a form. For example, you must enter only numbers in text, however, users may mistakenly enter unnecessary spaces or text mixed with other characters, which obviously does not meet the input requirements. Let's take a look at how to remove spaces on both sides of the string. Before Filtering: After filtering: HTML code:

<Div class = "main"> <input id = "userName" type = "text" placeholder = "Enter the user name"> <input id = "rule" type = "button" value = "filter"> </div>

CSS code:

html,body,div,input{margin:0;padding:0;}    .main{width:400px;height:auto;padding:0 15px;text-align:center;}    .main input{width:100%;height:35px;border:none;margin-top:20px;border-radius:5px;}    input[type="text"]{text-align:left;padding-left:15px;box-sizing:border-box;border:1px solid blue;}    input[type="button"]{width:50%;background:blue;}    @media only screen and (max-width: 415px) {      .main{width:100%;box-sizing:border-box;}    }

Js section:

Var userName = document. getElementById ('username'), rule = document. getElementById ('rule'), regexEmpty =/^ (\ s | \ u00A0) + | (\ s | \ u00A0) + $/g; rule. onclick = function () {userName. value = userName. value. replace (regexEmpty, ''); // replace console with regular expressions. log (userName. value );}

In the preceding section, the spaces (Implementation Code) on both sides of the string are all the content shared by the editor. I hope to give you a reference and support for the customer's house.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.