Javascript legal verification js data verification, jsemail verification, jsurl verification, js length verification, js digital verification, etc. (pop-up dialog box form)
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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}NoThe 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}NoThe 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}NoReturns 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.
* @ 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}Less than or greater than the maximum valueReturns 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)
* 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}No"6 to 20 characters in length and consisting 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 to be positioned by the cursorOnly IDS can be received
* @ 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)
*
* @ 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}Smaller than or greater than the maximum lengthReturns 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)
* @ 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}Longer than the given lengthReturns 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 to be positioned by the cursorOnly IDS can be received
* @ Return {Boolean}The character is null.Returns false;
*/
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'); // document 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;
}
}