JS Common Form Verification Method summary _javascript skill

Source: Internet
Author: User
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Form Validation </title>
<script language= "JavaScript" >
function Check_1 (param) {//NOT NULL
if (param== "" | | Param==null) {return false;} Else{return true;}
}
function check_2 (param) {//length limit, letters 10, Chinese characters are 10
if (param.length>10) {return false;} Else{return true;}
}
function Check_3 (param) {//can only enter Chinese characters
var pattern=/^[\u4e00-\u9faf]+$/;
var flag = pattern.test (param);
if (Flag==false) {return false;} Else{return true;}
}
function Check_4 (param) {//only digits can be entered
var pattern=/^[0-9]+$/;
var flag = pattern.test (param);
if (Flag==false) {return false;} Else{return true;}
}
function Check_5 (param) {//Only numeric letter underline can be entered
var pattern=/^[0-9a-za-z_]+$/;
var flag = pattern.test (param);
if (Flag==false) {return false;} Else{return true;}
}
function Check_6 (param) {//Mailbox format verification
var pattern=/^ ([a-za-z0-9]+[_|\_|\.]?) *[a-za-z0-9]+@ ([a-za-z0-9]+[_|\_|\.]?) *[a-za-z0-9]+\. [A-za-z] {2,3}$/;
var flag = pattern.test (param);
if (Flag==false) {return false;} Else{return true;}
}
function check_7 (param) {//URL format validation
var pattern=/^ (https|http|ftp|rtsp|mms)?: \ /\/)? ([0-9a-z_!~* ' (). &=+$%-]+:)? [0-9a-z_!~* ' (). &=+$%-]+@)? (([0-9]{1,3}\.) {3} [0-9] {1,3}| ([0-9a-z_!~* ' ()-]+\.) * ([0-9a-z][0-9a-z-]{0,61})? [0-9a-z]\. [A-z] {2,6}) (: [0-9]{1,4})? ((\/?)| (\/[0-9a-z_!~* ' ().;?: @&=+$,%#-]+) +\/?) $/;
var flag = pattern.test (param);
if (Flag==false) {return false;} Else{return true;}
}
function Check () {
var Value=document.getelementbyid ("TestValue"). Value;
var result=check_7 (value); Set the name of the validation function here, I wrote check_1~check_7 seven common forms validation functions
if (Result==false) {
document.getElementById ("TestValue"). style.border= "2px solid red";
}else{
document.getElementById ("TestValue"). style.border= "2px solid green";
document.getElementById ("TestValue"). style.border= "";
}
}
</script>
<body>
<div style= "text-align:center;padding:200px 0;" >
Please enter: <input id= "TestValue" name= "class=" "type=" text "style=" width:200px;height:30px; " ></input>
<input id= "Name=" "class=" "type=" button "value=" click Verify "style=" width:70px;height:30px; "onclick=" Check () ">< /input>
</div>
</body>

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.