Javascript Form Validation instance code before submitting

Source: Internet
Author: User

Cases

Add the onsubmit attribute to the form or the Add OnClick property of the Submit button, the value is return check (), the check is a JavaScript function, the validation table is single, no call, even if the validation fails will be submitted. In the check function, the validation failure returns false and returns true successfully.
Code:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<body>
<form action= "index.php" method= "Get" Name= "Form1" >
Name: <input type= "text" name= "name" id= "name"/><br/>
Ages: <input type= "text" name= "age" id= "aged"/><br/>
<input type= "Submit" onclick= "return Check ()"/>

</form>
<script language= "javascript" type= "Text/javascript" >
function Check () {
var age=parseint (Document.form1.age.value);
if (age<18) {
Alert ("Less than 18");
return false;
}else{
return true;
}
}
</script>
</body>

Example 2

Validating plug-ins with jquery forms validator

Call simple, style generic, you don't have to write style. A very useful Web page forms validation control.


First look at the effect, the following figure:

The code is as follows Copy Code


<script type=text/javascript src= "Js/validator.js" ></SCRIPT>
<script type=text/javascript>
var a = Validator ({
"Form": "Validator-form",
Ajaxsubmit:true,
Beforsubmit:function () {
Alert ("Function executed before form submission");
},
Aftersubmit:function () {
Alert ("The data returned after the AJAX submission is:" +this.responsetext);
}
}). Add ({
"Target": "username",
"Rule_type": "username",
' Action ': ' 2.php ',
' Tips ': ' Say something good '
}). Add ({
"Target": "Password",
"Rule_type": "Password"
}). Add ({
"Target": "Confirm-password",
"Rule_type": "Password",
"Sameto": "Password",
' ERROR ': ' You fill in the wrong password or different from the original password '
}). Add ({
"Target": "Age",
"Rule_type": "number| | Empty ",
"Error": ' Although this thing is not required, but since you fill in, you fill it out right. '
}). Add ({
"Target": "Birthday",
"Rule_type": "date| | Empty
}). Add ({
"Target": "Id-card",
"Rule_type": "idcard| | Empty
}). Add ({
"Target": "Email",
"Rule_type": "Email"
}). Add ({
"Target": "QQ",
"Rule_type": "qq| | Empty
}). Add ({
"Target": "Telephone",
"Rule_type": "phone| | Empty
}). Add ({
"Target": "Mobile",
"Rule_type": "mobile| | Empty
}). Add ({
"Target": "Phone",
"Rule_type": "phone| | Mobile ",
"Tips": "Please enter your mobile phone or landline phone number!" "
}). Add ({
"Target": "Zip-code",
"Rule_type": "zip| | Empty
}). Add ({
"Target": "Ip-address",
"Rule_type": "ip| | Empty
}). Add ({
"Target": "url",
"Rule_type": "url| | Empty
}). Add ({
"Target": "Message",
"Rule_type": "Require"
});

var reset = document.getElementById (' reset ');
var del = document.getElementById (' del ');
Reset.onclick = function () {
A.reset ();
}
Del.onclick = function () {
A.remove (' username ');
}

</SCRIPT>

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.