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.