This example describes a common JS validation script. Share to everyone for your reference, specific as follows:
/*--------------function Retrieval--------------trim function: Trim () LTrim () RTrim () Verify string is null: Checkisnotempty (str) School Verify that the string is integral: Checkisinteger (str) checksum integer Minimum: checkintegerminvalue (str,val) Check integer maximum: checkintegermaxval UE (STR,VAL) verifies that the integer is not a negative number: Isnotnegativeinteger (str) verifies that the string is a floating-point type: checkisdouble (str) verifies the minimum value of a floating-point type: Checkdo Ubleminvalue (str,val) checksum floating-point maximum: Checkdoublemaxvalue (str,val) Verify that the floating-point type is non-negative: isnotnegativedouble (str) Verify that the string is a day Phase: Checkisvaliddate (str) Check two-date sequence: Checkdateearlier (strstart,strend) Check whether the string is email type: checkemail (str) School Check whether the string is Chinese: checkischinese (str) calculates the length of a string, a character two characters: Reallength () Verify that the string conforms to a custom regular expression: Checkmask (str,pat) gets the suffix name of the file: Getfilepostfix (ofile)--------------function Retrieval--------------/* String.prototype.trim = function () {return this.rep Lace (/(^[\\s]*) | (
[\\s]*$)/g, "");
String.prototype.lTrim = function () {return this.replace (/(^[\\s]*)/g, "");} String.prototype.rTrim = Function () {return this.replace (/[\\s]*$)/g, "");}
function Checkisnotempty (str) {if (Str.trim () = "") return false;
else return true;
}//~~~ function Checkisinteger (str) {//If empty, return true by validating if (str = = ""). if (/^ (\\-?)
(\\d+) $/.test (str)) return true;
else return false;
}//~~~ function Checkintegerminvalue (str,val) {//If empty, return true by validating if (str = = "");
if (typeof (Val)!= "string") val = val + "";
if (Checkisinteger (str) = = True) {if (parseint (str,10) >=parseint (val,10)) return true;
else return false;
else return false;
}//~~~ function Checkintegermaxvalue (str,val) {//If empty, return true by validating if (str = = "");
if (typeof (Val)!= "string") val = val + "";
if (Checkisinteger (str) = = True) {if (parseint (str,10) <=parseint (val,10)) return true;
else return false;
else return false;
}//~~~ function Isnotnegativeinteger (str) {//If empty, check if (str = = "") return true;
if (Checkisinteger (str) = = True) {if (parseint (str,10) < 0) return false;
else return true;
else return false;
}//~~~ function checkisdouble (str) {//If empty, return true by validating if (str = = "").
If it is an integer, verify the validity of the integer if (Str.indexof (".") = = 1) {if (Checkisinteger (str) = = true) return true;
else return false; else {if (/^ (\\-?) (\\d+) (. {1})
(\\d+) $/g.test (str)) return true;
else return false;
}//~~~ function Checkdoubleminvalue (str,val) {//If empty, return true by validating if (str = = "").
if (typeof (Val)!= "string") val = val + "";
if (checkisdouble (str) = = True) {if (parsefloat (str) >=parsefloat (val)) return true;
else return false;
else return false;
}//~~~ function Checkdoublemaxvalue (str,val) {//If empty, return true by validating if (str = = "");
if (typeof (Val)!= "string") val = val + ""; if (checkisdouble (str) = = True) {if (Parsefloat (str) <=parsefloat (val)) return true;
else return false;
else return false;
}//~~~ function isnotnegativedouble (str) {//If empty, return true by validating if (str = = "").
if (checkisdouble (str) = = True) {if (parsefloat (str) < 0) return false;
else return true;
else return false;
}//~~~ function Checkisvaliddate (str) {//If empty, return true by validating if (str = = ""). var pattern =/^ ((\\d{4}) | (
\\D{2}))-(\\d{1,2})-(\\d{1,2}) $/g;
if (!pattern.test (str)) return false;
var arrdate = Str.split ("-");
if (parseint (arrdate[0],10) <) arrdate[0] = + parseint (arrdate[0],10) + "";
var date = new Date (Arrdate[0], (parseint (arrdate[1],10)-1) + "", arrdate[2]); if (date.getyear () = = Arrdate[0] && date.getmonth () = = (parseint (arrdate[1],10)-1) + "" && date.getd
ate () = = Arrdate[2]) return true;
else return false; }//~~~ function Checkdateearlier (strstart,strend) {if (checkisvaliddate(Strstart) = = False | |
Checkisvaliddate (strend) = = false) return false; If one of the inputs is empty, check if ((Strstart = "") | |
(Strend = ""))
return true;
var arr1 = Strstart.split ("-");
var arr2 = Strend.split ("-");
var date1 = new Date (Arr1[0],parseint (Arr1[1].replace (/^0/, ""),-1,arr1[2]);
var date2 = new Date (Arr2[0],parseint (Arr2[1].replace (/^0/, ""),-1,arr2[2]);
if (arr1[1].length = = 1) arr1[1] = "0" + arr1[1];
if (arr1[2].length = = 1) arr1[2] = "0" + arr1[2];
if (arr2[1].length = = 1) arr2[1] = "0" + arr2[1];
if (arr2[2].length = = 1) arr2[2]= "0" + arr2[2];
var D1 = arr1[0] + arr1[1] + arr1[2];
var D2 = arr2[0] + arr2[1] + arr2[2];
if (parseint (d1,10) > parseint (d2,10)) return false;
else return true;
}//~~~ function Checkemail (str) {//If empty, return true by validating if (str = = ""). if (Str.charat (0) = = "." | | Str.charat (0) = "@" | | str.indexof (\ ' @\ ', 0) = = 1 | | str.indexof (\ '. \ ', 0) = =-1 | | str. LastIndexOf ("@") = =str.length-1 | |
Str.lastindexof (".") = = Str.length-1) return false;
else return true;
}//~~~ function Checkischinese (str) {//If the value is null, return True by validating if (str = = ""); var pattern =/^ ([\ a-\ 龥]|[ \︰-\??])
*$/gi;
if (Pattern.test (str)) return true;
else return false; }//~~~ String.prototype.realLength = function () {return this.replace (/[^\\x00-\\xff]/g, "* *"). Length;} function
Checkmask (Str,pat) {//If the value is null, return True by validating if (str = = "");
var pattern = new RegExp (Pat, "GI")-if (Pattern.test (str)) return true;
else return false;
}//~~~ function Getfilepostfix (ofile) {if (ofile = = null) return null; var pattern =/(. *) \.
(. *) $/gi;
if (typeof (ofile) = = "Object") {if (Ofile.value = null | | ofile.value = = "") return null;
var arr = pattern.exec (ofile.value);
return regexp.$2;
else if (typeof (ofile) = = "string") {var arr = pattern.exec (ofile);
return regexp.$2;
else return null;
}
I hope this article will help you with your JavaScript programming.