CopyCode The Code is as follows :/**
* Get the length of Chinese and English characters
* @ Param {} Str
* @ Return {}
*/
Function lengthb (STR ){
VaR p1 = new Regexp ('% u ..', 'G ')
VaR P2 = new Regexp ('%.', 'G ')
Return escape (STR). Replace (P1, ''). Replace (P2,''). Length
}
/**
* Filter all space characters
* @ Param {object} Str
*/
Function jstrim (STR ){
STR + = "";
While (Str. charat (0) = '') | (Str. charat (0) = '??? ') | (Escape (Str. charat (0) =' % u3000 '))
STR = Str. substring (1, str. Length );
While (Str. charat (Str. Length-1) = '') | (Str. charat (Str. Length-1) = '??? ') | (Escape (Str. charat (Str. Length-1) =' % u3000 '))
STR = Str. substring (0, str. Length-1 );
Return STR;
}
/**
* Filter the intermediate string and multiple Spaces
* @ Param {object} inputstring
*/
Function trim (inputstring ){
If (typeof inputstring! = "String "){
Return inputstring;
}
VaR retvalue = inputstring;
VaR CH = retvalue. substring (0, 1 );
While (CH = ""){
// Check the space at the beginning of the string
Retvalue = retvalue. substring (1, retvalue. Length );
Ch = retvalue. substring (0, 1 );
}
Ch = retvalue. substring (retvalue. Length-1, retvalue. Length );
While (CH = ""){
// Check the space at the end of the string
Retvalue = retvalue. substring (0, retvalue. Length-1 );
Ch = retvalue. substring (retvalue. Length-1, retvalue. Length );
}
While (retvalue. indexof ("")! =-1 ){
// Convert multiple spaces in the middle of the text into one space
Retvalue = retvalue. substring (0, retvalue. indexof ("") + retvalue. substring (retvalue. indexof ("") + 1, retvalue. Length );
}
Return retvalue;
}
/**
* Filter the string to specify the content to be filtered. if the content is empty, the default value is '~! @ # $ % ^ & * ()-+ ."
* @ Param {object} Str
* @ Param {object} filterstr
*
* @ Return: contains the filtered content. True is returned; otherwise, false is returned;
*/
Function filterstr (STR, filterstr ){
Filterstr = ""? "'~! @ # $ % ^ & * ()-+. \ "": Filterstr
VaR ch;
VaR I;
VaR temp;
VaR error = false; // returns true if it contains invalid characters.
For (I = 0; I <= (filterstr. Length-1); I ++ ){
Ch = filterstr. charat (I );
Temp = Str. indexof (CH );
If (temp! =-1 ){
Error = true;
Break;
}
}
Return Error;
}
/**
* Filter the specified content string
* @ Param {object} STR check the string
* @ Param {object} filterstr: filter the string. if the content is empty, '~!' is filtered by default '~! @ # $ % ^ & * ()-+ ."
* @ Param {object} alertstr pop-up dialog content
* @ Param {object} idstr error returned error field ID
*/
Function isfilterstr (STR, filterstr, alertstr, idstr ){
Alertstr = "sorry, your input" + alertstr + "does not allow" + filterstr + "invalid character ";
If (filterstr (STR, filterstr ))
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether the URL is used
* @ Param {} str_url
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> NO </B> the URL returns false;
*/
Function isurl (str_url, alertstr, idstr) {// verify the URL
Alertstr = alertstr + "Incorrect format! ";
VaR strregex = "^ (HTTPS | HTTP | FTP | RTSP | MMS )? : //) "+
"? ([0-9a-z _!~ * '(). & =+ $ %-] + :)? [0-9a-z _!~ * '(). & =+ $ %-] + @)? "// FTP user @
+
"([0-9] {1, 3} \.) {3} [0-9] {1, 3}" // URL in IP Format-199.194.52.184
+
"|" // Allow IP and domain (domain name)
+
"([0-9a-z _!~ * '()-] + \.) * "// Domain name-www.
+
"([0-9a-z] [0-9a-z-] {0, 61 })? [0-9a-z] \. "// second-level domain name
+
"[A-Z] {2, 6})" // first level domain-. com or. Museum
+
"(: [0-9] {1, 4 })? "// Port-: 80
+
"((/?) | "// A slash isn' t required if there is no file name
+
"(/[0-9a-z _!~ *'().;? : @ & =+ $, % #-] +) + /?) $ ";
VaR Re = new Regexp (strregex );
If (! Re. Test (str_url ))
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether the email is used
* @ Param {} Str
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> NO </B> the email returns false;
*/
Function isemail (STR, alertstr, idstr ){
Alertstr = alertstr + "Incorrect format! ";
VaR Re =/^ ([a-zA-Z0-9] + [_ | \-| \.]?) * [A-zA-Z0-9] + @ ([a-zA-Z0-9] + [_ | \-| \.]?) * [A-zA-Z0-9] + \. [A-Za-Z] {2, 3} $ /;
If (! Re. Test (STR ))
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether it is a number.
* @ Param {} Str
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> NO </B>: false;
*/
Function isnum (STR, alertstr, idstr ){
Alertstr = alertstr + "should be composed of numbers! ";
VaR Re =/^ [\ D] + $/
If (! Re. Test (STR ))
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether the value is within the specified range <br>
* @ Param {} str_num
* @ Param {} morelen should be a value greater than or equal
* @ Param {} lesslen should be a value smaller than or equal
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> A number less than the minimum value or greater than the maximum value </B> returns false;
*/
Function israngenum (str_num, morelen, lesslen, alertstr, idstr ){
Isnum (str_num, alertstr, idstr );
If (morelen! = ""){
Alertstr = alertstr + "value cannot be less than" + morelen;
If (str_num <morelen)
This. alertandrfalse (alertstr, idstr );
}
If (lesslen! = ""){
Alertstr = alertstr + "value cannot be greater than" + lesslen;
If (str_num> lesslen)
This. alertandrfalse (alertstr, idstr );
}
If (morelen = "" & lesslen = "")
This. alertandrfalse ("the maximum and minimum lengths are not defined! ", Idstr );
}
/**
* Check whether it is a qualified string (Case Insensitive) <br>
* A string of 6 to 20 characters consisting of a-z0-9 _
* @ Param {} STR check string
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> not </B> "6 to 20 characters in length and composed of a-z0-9 _" returns false;
*/
Function islicit (STR, alertstr, idstr ){
Alertstr = "sorry," + alertstr + "cannot be blank and can only contain 6 to 20 digits ranging from 0 to 9 A to Z! ";
VaR Re =/^ [_ 0-9a-za-z] {6, 20} $/
If (! Re. Test (STR ))
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether the two strings are equal
* @ Param {} str1 the first string
* @ Param {} str2 second string
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} returns false if the string is not equal;
*/
Function isstrequals (str1, str2, alertstr, idstr ){
Alertstr = "secondary" + alertstr + "inconsistent! ";
If (str1! = Str2)
This. alertandrfalse (alertstr, idstr );
}
/**
* Check whether the string is within the specified length range (Chinese characters are counted in 2 bytes) <br>
*
* @ Param {} STR check characters
* @ Param {} The morelen length must be greater than or equal
* @ Param {} lesslen should be less than or equal to the length
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> A number smaller than or greater than the minimum length </B> returns false;
*/
Function isrange (STR, morelen, lesslen, alertstr, idstr ){
VaR strlen = lengthb (STR );
If (morelen! = ""){
Alertstr = alertstr + "should be greater than or equal to" + morelen + "bytes in length! ";
If (strlen <morelen)
This. alertandrfalse (alertstr, idstr );
}
If (lesslen! = ""){
Alertstr = alertstr + "should be less than or equal to" + lesslen + "bytes in length! ";
If (strlen> lesslen)
This. alertandrfalse (alertstr, idstr );
}
If (morelen = "" & lesslen = "")
This. alertandrfalse ("the maximum and minimum lengths are not defined! ", Idstr );
}
/**
* Check whether the string is within the specified length range (Chinese characters are counted in 2 bytes) <br>
* @ Param {} STR string
* @ Param {} lesslen is less than or equal to the length
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> A number greater than the given length </B> returns false;
*/
Function isless (STR, lesslen, alertstr, idstr ){
Isrange (STR, "", lesslen, alertstr, idstr );
}
/**
* Check characters are not empty
* @ Param {} Str
* @ Param {} the content of the alertstr pop-up field
* @ Param {} idstr the ID of the field positioned by the cursor <B> only the ID can be received </B>
* @ Return {Boolean} <B> the character is null </B>. False is returned;
*/
Function isempty (STR, alertstr, idstr ){
Alertstr = alertstr + "cannot be blank! ";
If (STR = "")
This. alertandrfalse (alertstr, idstr );
}
/**
* The Alert box is displayed, and the error field is located.
* @ Param {} alertstr pop-up warning box content
* @ Param {} idstr returns the field focus
* @ Return {Boolean} returns false.
*/
Function alertandrfalse (alertstr, idstr ){
Alert (alertstr );
Document. getelementbyid (idstr). Focus ();
Throw "valueerr ";
}
// JavaScript document
Call example
Function checkform (){
Try {
VaR Title = trim (document. getelementbyid ('title '));// Article Title
Isempty (title, "article title", "title"); // cannot be empty
Isless (title, 100, "article title", "title"); // the title should be less than 100 characters long
Isfilterstr (title ,"~ '! @ # $ % ^ & * ()-= _ + [] {}|\; ': \ ",./<>? "," Article title "," title ") // The title cannot contain these illegal characters
} Catch (ERR ){
If (ERR = "valueerr ")
Return false;
}
}