In daily work, filtering some special characters in the form is a very common function, such as requiring simple numbers to be entered in text, but users sometimes mistakenly enter some extra spaces or text mixed with other characters, which obviously does not meet the input requirements. Let’s learn how to remove the spaces on both sides of the string. Before filtering: After filtering: HTML code:
<div> <input id="userName" type="text" placeholder="Please enter username"> <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 part:
var userName = document.getElementById('userName'), rule = document.getElementById('rule'), regexEmpty = /^(/s|/u00A0)+|(/s|/u00A0)+$/g; rule.onclick = function (){ userName.value = userName.value.replace(regexEmpty,''); //regularly replace console.log(userName.value); }The above article removes the spaces on the left and right sides of the string (implementation code) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.