Simple JS Form Validation effect code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.