In js, spaces and spaces are two different types of conditions. Spaces occupy one character space. Many programmers can directly submit a space if they enter a few spaces during judgment, to filter spaces in js, we only need to use replace for replacement and then judge.
Test instance
1. Only spaces cannot be entered.
| The Code is as follows: |
Copy code |
<Input type = "text" onkeyup = "this. value = this. value. replace (/^ + | + $/g,'') "> |
2. Do not enter spaces or commas (,) in English.
| The Code is as follows: |
Copy code |
<Input type = "text" onkeyup = "this. value = this. value. replace (/[,]/g,'') "> |
3,
| The Code is as follows: |
Copy code |
<Script type = "text/javascript"> Function myfunction (){ Var a = $ ("text1"). val (); If ($. trim (a) = ""){ Alert ("cannot be blank! "); Return false; } } </Script> |
Cannot contain all spaces
| The Code is as follows: |
Copy code |
<Html> <Head> <Script> Function checkBlankSpace (str ){ While (str. lastIndexOf ("")> = 0 ){ Str = str. replace ("",""); } If (str. length = 0 ){ Alert ("the input cannot be empty "); } } Function test (){ Var testStr = document. getElementById ("test"). value; CheckBlankSpace (testStr ); } </Script> </Head> <Body> <Input type = "text" id = "test"/> <Input type = "button" value = "test" onclick = "test ()"> </Body> </Html> |