Simple JS Form Verification code:
Form validation is almost indispensable, some form validation is done in the background, and some are using JavaScript in front of the basic verification, which can effectively alleviate the pressure on the server, the following describes the simplest form of JS implementation validation. The code example is as follows:
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.softwhy.com/" /><title>Ant Tribe</title><Scripttype= "Text/javascript"> functionChkform () {if(document.getElementById ("username"). Value=="") {alert ("the user name cannot be empty! "); return false; } if(document.getElementById ("PW"). Value=="") {alert ("The password cannot be empty! "); return false; } } </Script> </Head> <Body> <formAction= "Http://www.softwhy.com"name= "MyForm"> <Table> <TR> <TD>User name:</TD> <TD><inputtype= "text"name= "username"ID= "username" /></TD> </TR> <TR> <TD>Password:</TD> <TD><inputtype= "Password"name= "PW"ID= "PW" /></TD> </TR> <TR> <TDcolspan= "2"><inputtype= "Submit"value= "Submit"></TD> </TR> </Table> </form> </Body> </HTML>
The above code implements the most basic form validation, that is, do not allow the form content is empty, the following is a brief introduction of its implementation process:
One. In JavaScript code, create a Chkform () function to validate the form, and here's a brief introduction to the secondary function:
document.getElementById ("username"). Value
The above code can get the value of the object with ID username, and then through the IF statement to determine whether this value is empty, if empty then return false, this statement is very important, otherwise even if the form content is empty, the form will be submitted, also can not achieve the validation effect, The same is true of the second if Judgment statement, which is not covered here.
Two. onclick= "return Chkform ()", the function of this statement is when the click submit button will be executed when the Chkform () function is used to verify the form, here special emphasis do not forget to add return, for specific reasons can be see onclick= " Return Chkform () "Return in the role."
The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=6179
For more information, refer to: http://www.softwhy.com/javascript/
Simple JS Form Validation effect code