! Remove spaces at both ends of a string
If you use the traditional method, you may need to use the following method.
// Clear spaces on the left
Function js_ltrim (deststr)
{
If (deststr = null) return "";
Var pos = 0;
Var retStr = new String (deststr );
If (retStr. lenght = 0) return retStr;
While (retStr. substring (pos, pos + 1) = "") pos ++;
RetStr = retStr. substring (pos );
Return (retStr );
}
// Clear spaces on the right
Function js_rtrim (deststr)
{
If (deststr = null) return "";
Var retStr = new String (deststr );
Var pos = retStr. length;
If (pos = 0) return retStr;
While (pos & retStr. substring (pos-1, pos) = "") pos --;
RetStr = retStr. substring (0, pos );
Return (retStr );
}
// Clear spaces on the left and right
Function js_trim (deststr)
{
If (deststr = null) return "";
Var retStr = new String (deststr );
Var pos = retStr. length;
If (pos = 0) return retStr;
RetStr = js_ltrim (retStr );
RetStr = js_rtrim (retStr );
Return retStr;
}
Use a regular expression to remove spaces on both sides.
String. prototype. trim = function ()
{
Return this. replace (/(^ \ s *) | (\ s * $)/g ,"");
}
That's all you need to do,
It can be seen that regular expressions save a considerable amount of code writing.
! Mobile phone number verification
If you use the traditional verification method, you must complete at least the following three steps,
(1). Whether it is a number
(2). Whether it is 11 digits
(3) Whether the third digit of a number is 5, 6, 7, 8, 9
If regular expression verification is used, the following code is required:
Function checkMobile1 (form)
{
If (form. mobile. value> "")
{
Var reg =/13 [5, 6, 7, 8, 9] \ d {8 }/;
If (form. mobile. value. match (reg) = null)
{
Alert ("enter the correct mobile phone number! ");
Form. mobile. focus (); return false;
}
}
Return true;
}
From the code above, we can see that you only need to define a var reg =/13 [, 9] \ d {8}/to verify the validity of the mobile phone number.
! URL validation,
Condition: it must start with http: // or https: //, and the port number must be between 1 and 65535. The following code has completed the validity verification.
// Obj: Data Object
// DispStr: the error message is a string.
Function checkUrlValid (obj, dispStr)
{
If (obj = null)
{
Alert ("the input object is empty ");
Return false;
}
Var str = obj. value;
Var urlpatern0 =/^ https? : \/. + $/I;
If (! Urlpatern0.test (str ))
{
Alert (dispStr + "invalid: it must start with 'HTTP: \/'or 'https! ");
Obj. focus ();
Return false;
}
Var urlpatern2 =/^ https? : \/([A-zA-Z0-9 _-]) + (\.)?) * (: \ D + )?. + $/I;
If (! Urlpatern2.test (str ))
{
Alert (dispStr + "the port number must be a number and must be between 1! ");
Obj. focus ();
Return false;
}
Var urlpatern1 =/^ https? : \/([A-zA-Z0-9 _-]) + (\.)?) * (: \ D + )? (\/((\.)? (\?)? =? &? [A-zA-Z0-9 _-] (\?) *) * $/I;
If (! Urlpatern1.test (str ))
{
Alert (dispStr + "illegal, please check! ");
Obj. focus ();
Return false;
}
Var s = "0 ";
Var t = 0;
Var re = new RegExp (": \ d +", "ig ");
While (arr = re.exe c (str ))! = Null)
{
S = str. substring (RegExp. index + 1, RegExp. lastIndex );
If (s. substring (0, 1) = "0 ")
{
Alert (dispStr + "the port number cannot start with 0! ");
Obj. focus ();
Return false;
}
T = parseInt (s );
If (t <1 || t> 65535)
{
Alert (dispStr + "the port number must be a number and must be between 1! ");
Obj. focus ();
Return false;
}
}
Return true;
}
There seems to be a lot of code for url validation. This is because an error is prompted. Otherwise, you only need to var urlpatern1 =/^ https? : \/([A-zA-Z0-9 _-]) + (\.)?) * (: \ D + )? (\/((\.)? (\?)? =? &? [A-zA-Z0-9 _-] (\?) *) * $/I; the legality of the url can be verified.
Javascript Regular Expression Test
/*************************************** **************************************** **
* EO_JSLib.js
* Javascript Regular Expression Test
**************************************** **************************************** **/
// Check whether it is composed of digits
Function isDigit (s)
{
Var patrn =/^ [0-9] {1, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Check Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".
Function isRegisterUserName (s)
{
Var patrn =/^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Check user name: only 1-30 strings starting with letters can be entered
Function isTrueName (s)
{
Var patrn =/^ [a-zA-Z] {1, 30} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Password verification: only 6-20 letters, numbers, and underscores can be entered
Function isPasswd (s)
{
Var patrn =/^ (\ w) {6, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the phone number and fax number. The phone number can start with "+" and contain "-" in addition to numbers.
Function isTel (s)
{
// Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) {1, 12}) + $ /;
Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the mobile phone number. It must start with a number and can contain "-" in addition to a number.
Function isMobil (s)
{
Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the zip code
Function isPostalCode (s)
{
// Var patrn =/^ [a-zA-Z0-9] {3, 12} $ /;
Var patrn =/^ [a-zA-Z0-9] {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the search keyword
Function isSearch (s)
{
Var patrn =/^ [^ '~! @ # $ % ^ & * () + = |\\] [\] \{\}:;' \,. <>/?] {1} [^ '~! @ $ % ^ & () + =|\\] [\] \{\}:; '\,. <>?] {0, 19} $ /;
If (! Patrn.exe c (s) return false
Return true
}
Function isIP (s) // by zergling
{
Var patrn =/^ [0-9.] {1, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}