1. The form item cannot be blank:
<Script language = "java script"> <! -- Function CheckForm () { If (document. form. name. value. length = 0 ){ Alert ("enter your name! "); Document. form. name. focus (); Return false; } Return true; } --> </Script> |
2. Compare whether the values of the two form items are the same:
<Script language = "java script"> <! -- Function CheckForm () If (document. form. PWD. value! = Document. form. PWD_Again.value) { Alert ("the password you entered twice is different! Enter again ."); Document. ADDUser. PWD. focus (); Return false; } Return true; } --> </Script>
|
3. Form items can only be numbers and "_", used for phone/Bank Account Verification, and can be extended to domain name registration and so on:
<Script language = "java script"> <! -- Function isNumber (String) { Var Letters = "1234567890-"; // you can add input values by yourself. Var I; Var c; If (String. charAt (0) = '-') Return false; If (String. charAt (String. length-1) = '-') Return false; For (I = 0; I <String. length; I ++) { C = String. charAt (I ); If (Letters. indexOf (c) <0) Return false; } Return true; } Function CheckForm () { If (! IsNumber (document. form. TEL. value )){ Alert ("your phone number is invalid! "); Document. form. TEL. focus (); Return false; } Return true; } --> </Script> |
4. Input numeric values/length limitation for form items:
<Script language = "java script"> <! -- Function CheckForm () { If (document. form. count. value & gt; 100 | Document. form. count. value <1) { Alert ("the input value cannot be less than zero or greater than 100! "); Document. form. count. focus (); Return false; } If (document. form. MESSAGE. value. length <10) { Alert ("the input text is less than 10! "); Document. form. MESSAGE. focus (); Return false; } Return true; } // --> </Script> |
5. Determination of validity of Chinese/English/numbers/email addresses:
<Script LANGUAGE = "java script"> <! -- Function isEnglish (name) // english value detection { If (name. length = 0) Return false; For (I = 0; I <name. length; I ++ ){ If (name. charCodeAt (I) & gt; 128) Return false; } Return true; } Function isChinese (name) // Chinese value detection { If (name. length = 0) Return false; For (I = 0; I <name. length; I ++ ){ If (name. charCodeAt (I) & gt; 128) Return true; } Return false; } Function isMail (name) // E-mail value detection { If (! IsEnglish (name )) Return false; I = name. indexOf (""); J = name dot lastIndexOf (""); If (I =-1) Return false; If (I! = J) Return false; If (I = name dot length) Return false; Return true; } Function isNumber (name) // value detection { If (name. length = 0) Return false; For (I = 0; I <name. length; I ++ ){ If (name. charAt (I) <"0" | name. charAt (I)> "9 ") Return false; } Return true; } Function CheckForm () { If (! IsMail (form. Email. value )){ Alert ("your email is invalid! "); Form. Email. focus (); Return false; } If (! IsEnglish (form. name. value )){ Alert ("the English name is invalid! "); Form. name. focus (); Return false; } If (! IsChinese (form. cnname. value )){ Alert ("invalid Chinese name! "); Form. cnname. focus (); Return false; } If (! IsNumber (form. PublicZipCode. value )){ Alert ("the zip code is invalid! "); Form. PublicZipCode. focus (); Return false; } Return true; } // --> </Script>
|
6. Restrict characters that cannot be entered for form items:
<Script language = "java script"> <! -- Function contain (str, charset) // string contains the test function { Var I; For (I = 0; I <charset. length; I ++) If (str. indexOf (charset. charAt (I) >=0) Return true; Return false; } Function CheckForm () { If (contain (document. form. NAME. value, "% \ (\) >< ")) | (Contain (document. form. MESSAGE. value, "% \ (\) >< "))) { Alert ("illegal characters entered "); Document. form. NAME. focus (); Return false; } Return true; } // --> </Script>
|
- Javascript form verification (Part 1)
- How to dynamically change the image size through JavaScript
- Comprehensive list of practical JavaScript development tools
- Programming Language ranking in June: JavaScript is just getting started
- The essence of JavaScript Language