Javascript Form Verification length, javascript form
JavaScript can be used to verify the input data in the HTML form before the data is sent to the server.
What method should we use to remind users? You do not want to use the alert () Prompt box
Add an sqan label after the input field
<Input id = "phone" name = "phone" type = "text" size = "12" onBlur = "validateNonEmptyFun (this, document. getElementById ('phone _ help'); "/> <span id =" phone_help "class =" help "> </span> function validate_Length (inputFiled, helpText) {// if the input field content is empty, the if (inputFiled. value. length = 0) {if (helpText! = Null) helpText. innerHTML = "text box cannot be blank";} // if the input field is not empty, the else if (helpText! = Null) helpText. innerHTML = ""} </script>
HelpText is the input span object.
The span label is used to remind users. It does not block user vision as alert does.
Besides non-empty verification, there are also dimensional problems
Verify Data Length
<input id="phone" name="phone" type="text" size="12" onBlur="validate_Length(1,32,this,document.getElementById('phone_help'));" /><span id="phone_help" class="help"></span>
Here the parameter is changed to four, the first is the minimum length of the text, and the second is the maximum length of the text.
Function validate_Length (minLegth, maxlength, inputFiled, helpText) {if (inputFiled. value. length <minLegth | inputFiled. value. length> maxlength) {if (helpText! = Null) {helpText. innerHTML = "Enter the length of" + minLenght + "to" + maxLength + "text"; return false ;}} else if (helpText! = Null) {helpText. innerHTML = "" return true ;}}
Verify zip code
Function validate_ZipCode (inputFiled, helpText) {if (inputFiled. value. length! = 5) {if (helpText! = Null) helpText. innerHTML = "the zip code must be 5 characters long"; return false;} else if (isNaN (inputFiled. value) {if (helpText! = Null) helpText. innerHTML = "the zip code must be a number"; return false;} else if (helpText! = Null) {helpText. innerHTML = "" return true ;}}
Articles you may be interested in:
- Regular Expressions for JavaScript form verification [recommended]
- Practical JS Form Verification tips
- Javascript Regular Expression Form Verification Code
- Getting started with jquery validate. js Form Verification
- Simple js form verification functions
- Js uses regular expressions to verify the form (relatively complete resources)
- Javascript Form Verification example (javascript verification email)
- Extjs form common verification Summary
- Jquery Form Verification plug-in (jquery. validate. js)
- Form Verification of simple registration module made by javascript