js form validation method

 

1. Length restrictions

<p>1. 长度限制</p>
<form name=a onsubmit="return test()">
<textarea name="b" cols="40" rows="6" placeholder="不能超过50个字符!"></textarea>
<br />
<input type="submit" name="Submit" value="check">
</form>

<script language="javascript">
function test()
{
if(document.a.b.value.length>50)
{
alert("不能超过50个字符!");
document.a.b.focus();
return false;
}
}
</script>

 

2. Only Chinese characters 

<p>2. Only Chinese characters</p>
<input type="text" onblur="isChinese(this.value)" placeholder="Please enter Chinese!" />

<script language="javascript">
function isChinese (obj){
var reg=/^[\u0391-\uFFE5]+$/;
if(obj!=""&&!reg.test(obj)){
alert('Chinese must be entered!');
return false;
}
}

</script>

 

The structure of the HTML in the following demo is similar to the verification of the Chinese structure, the editor only writes js~~~

 

3. English letters only 

<script type="text/javascript">
//Validation can only be letters
function checkZm(zm){
var zmReg=/^[a-zA-Z]*$/;
if(zm!=""&&!zmReg. test(zm)){
alert("Only English letters!");
return false;
}
}
</script>

 

4. Only numbers 

<script language=javascript>
//Validation can only be a number
function checkNumber(obj){
var reg = /^[0-9]+$/;
if(obj!=""&&!reg.test(obj)){
alert('Only enter numbers!');
return false;
}
}
</script>

 

5. Only English letters and numbers 

<script type="text/javascript">
//Validation can only be letters and numbers
function checkZmOrNum(zmnum){
var zmnumReg=/^[0-9a-zA-Z]*$/;
if(zmnum!="" &&!zmnumReg.test(zmnum)){
alert("You can only enter letters or numbers, please re-enter");
return false;
}
}
</script>

 

6. Check the time size (compared with the current time)

 

<script type="text/javascript">
//Check the time size (compared with the current time)
function checkDate(obj){
var obj_value=obj.replace(/-/g,"/");//Replace characters, change into a standard format (the inspection format is: '2009-12-10')
// var obj_value=obj.replace("-", "/");//replace characters into a standard format (the inspection format is: '2010 -12-10 11:12')
var date1=new Date(Date.parse(obj_value));
var date2=new Date();//Get today's date
if(date1>date2){
alert("Cannot be greater than the current date time!");
return false;
}
}
</script>

 

7. Block keywords (block *** and **** here)

<script type="text/javascript">
function test(obj) {
if((obj.indexOf ("***") == 0)||(obj.indexOf ("****") == 0 )){
alert("Block keywords (block *** and **** here)!");
return false;}
}
</script>

 

8. Is the password the same after entering the password twice? 

<script type="text/javascript">
function check(){
with(document.all){
if(input1.value!=input2.value)
{
alert("密码不一致")
input1.value = "";
input2.value = "";
}
else {
alert("密码一致");
document.forms[0].submit();
}
}
}
</script>

 

9. Form items cannot be empty 

<script language="javascript">
function CheckForm(obj)
{
if (obj.length == 0) {
alert("Name cannot be empty!");
return false;
}
return true;
alert("Name cannot be empty! ");
}
</script>

 

10. Email Verification

<script language="javascript">
function test(obj){
//validation for email
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[ a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA- Z]{2,3}$/;
if(!myreg.test(obj))
{
alert('Please enter a valid email address!');
return false;
}
}
</script>

 

11. Verify phone number

<script type="text/javascript">
function validatemobile(mobile)
{
if(mobile.length==0)
{
alert('Mobile number cannot be empty!');
return false;
}
if(mobile.length!=11 )
{
alert('Please enter a valid mobile phone number, it must be 11 digits!');
return false;
}

var myreg = /^(((13[0-9]{1})|(15[0-9] {1})|(18[0-9]{1}))+\d{8})$/;
if(!myreg.test(mobile))
{
alert('Please enter a valid mobile number!') ;
return false;
}
}
</script>

 

12. Verify ID number (must be a valid ID)

<script type="text/javascript">
// Constructor, the variable is the 15-digit or 18-digit ID number
function clsIDCard(CardNo) {
this.Valid=false;
this.ID15='';
this.ID18=' ';
this.Local='';
if(CardNo!=null)this.SetCardNo(CardNo);
}

// 设置身份证号码,15位或者18位
clsIDCard.prototype.SetCardNo = function(CardNo) {
this.ID15='';
this.ID18='';
this.Local='';
CardNo=CardNo.replace(" ","");
var strCardNo;
if(CardNo.length==18) {
pattern= /^\d{17}(\d|x|X)$/;
if (pattern.exec(CardNo)==null)return;
strCardNo=CardNo.toUpperCase();
} else {
pattern= /^\d{15}$/;
if (pattern.exec(CardNo)==null)return;
strCardNo=CardNo.substr(0,6)+'19'+CardNo.substr(6,9)
strCardNo+=this.GetVCode(strCardNo);
}
this.Valid=this.CheckValid(strCardNo);
}

// Verify the validity of the ID card
clsIDCard.prototype.IsValid = function() {
return this.Valid;
}

// Return the birthday string in the following format, 1981-10-10
clsIDCard.prototype.GetBirthDate = function() {
var BirthDate='';
if(this.Valid)BirthDate=this.GetBirthYear()+'-'+this .GetBirthMonth()+'-'+this.GetBirthDay();
return BirthDate;
}

// Returns the year in the birthday in the following format, 1981
clsIDCard.prototype.GetBirthYear = function() {
var BirthYear='';
if(this.Valid)BirthYear=this.ID18.substr(6,4);
return BirthYear;
}

// Return the month in the birthday, the format is as follows, 10
clsIDCard.prototype.GetBirthMonth = function() {
var BirthMonth='';
if(this.Valid)BirthMonth=this.ID18.substr(10,2);
if(BirthMonth .charAt(0)=='0')BirthMonth=BirthMonth.charAt(1);
return BirthMonth;
}

// Returns the day of the birthday in the following format, 10
clsIDCard.prototype.GetBirthDay = function() {
var BirthDay='';
if(this.Valid)BirthDay=this.ID18.substr(12,2);
return BirthDay;
}

// return gender, 1: male, 0: female
clsIDCard.prototype.GetSex = function() {
var Sex='';
if(this.Valid)Sex=this.ID18.charAt(16)%2;
return Sex;
}

// return 15-digit ID number
clsIDCard.prototype.Get15 = function() {
var ID15='';
if(this.Valid)ID15=this.ID15;
return ID15;
}

// return 18-digit ID number
clsIDCard.prototype.Get18 = function() {
var ID18='';
if(this.Valid)ID18=this.ID18;
return ID18;
}

// Return the province, for example: Shanghai, Zhejiang
clsIDCard.prototype.GetLocal = function() {
var Local='';
if(this.Valid)Local=this.Local;
return Local;
}

clsIDCard.prototype.GetVCode = function(CardNo17) {
var Wi = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);
var Ai = new Array('1','0','X','9','8','7','6','5','4','3','2');
var cardNoSum = 0;
for (var i=0; i<CardNo17.length; i++)cardNoSum+=CardNo17.charAt(i)*Wi[i];
var seq = cardNoSum%11;
return Ai[seq];
}

clsIDCard.prototype.CheckValid = function(CardNo18) {
if(this.GetVCode(CardNo18.substr(0,17))!=CardNo18.charAt(17))return false;
if(!this.IsDate(CardNo18.substr(6 ,8)))return false;
var aCity={11:"Beijing",12:"Tianjin",13:"Hebei",14:"Shanxi",15:"Inner Mongolia",21:"Liaoning",22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong" ",41:"Henan",42:"Hubei",43:"Hunan",44:"Guangdong",45:"Guangxi",46:"Hainan",50:"Chongqing",51:"Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan",81:"Hong Kong",82:"Macau",91:"国外"};
if(aCity[parseInt(CardNo18.substr(0,2))]==null)return false;
this.ID18=CardNo18;
this.ID15=CardNo18.substr(0,6)+CardNo18.substr(8,9);
this.Local=aCity[parseInt(CardNo18.substr(0,2))];
return true;
}

clsIDCard.prototype.IsDate = function(strDate) {
var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[2]-1, r[3]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[2]&&d.getDate()==r[3]);
}


function valiIdCard(idCard){
var checkFlag = new clsIDCard(idCard);
if (!checkFlag.IsValid()) {
alert("The ID number entered is invalid, please enter the real ID number!");
document.getElementByIdx( "idCard").focus();
return false;
}else{
alert("It is a valid ID card!");
}
}
</script>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024802&siteId=291194637