Common validation reg_javascript techniques in JavaScript

Source: Internet
Author: User
Tags port number

Good JS verification ~~~~~~~~~~~~~~~~~~~~~~~~~

Purpose: Verify the format of IP address

Input: Strip:ip Address

Returns: False if return true through validation;

*/function IsIP (strip) {if (IsNull (strip)) return false; var re=/^ (\d+) \. (\d+) \. (\d+) \. (\d+) $/g//matching the regular expression of an IP address if (re.test (StrIP)) {if (regexp.$1 <256 && regexp.$2<256 && regexp.$3< 
256 && regexp.$4<256) return true; 
return false; /* Purpose: Check that the input string is empty or all are space input: STR returns: If ALL is null return True, otherwise return false */function IsNull (str) {if (str = "") returns TR 
Ue 
var Regu = "^[]+$"; 
var re = new RegExp (Regu); 
return Re.test (str); /* Purpose: Check that the value of the input object conforms to the integer format input: The string returned by the STR input: false/function Isinteger (str) {var regu =/^[-]{0,1 If validation returns True 
}[0-9]{1,}$/; 
return Regu.test (str); /* Purpose: Check the input mobile phone number is correct input: s: string return: If you return true through validation, false/function Checkmobile (s) {var regu =/^[1][3][0-9]{9 
}$/; 
var re = new RegExp (Regu); 
if (Re.test (s)) {return true; 
}else{return false; Usage: Check that the input string conforms to a positive integer format input: S: string return: false/function Isnumber (s) {var regu = "^[0-9]+$" If passed by validation returns true 
; var re = new RegExp (regu); 
if (S.search (re)!=-1) {return true; 
else {return false; Use: Check whether the input string is a decimal number format, can be negative input: s: string return: If you return true by validation, false/function Isdecimal (str) {if IsInteg 
ER (str)) return true; var re =/^[-]{0,1} (\d+) [\.] 
+ (\d+) $/; 
if (Re.test (str)) {if (regexp.$1==0&&regexp.$2==0) return false; 
return true; 
else {return false; Use: Check that the value of the input object conforms to the port number format input: The string returned by the STR input: false/function isport (str) {return (isnumber) if validation returns True 
R) && str<65536); /* Purpose: Check that the value of the input object conforms to the e-mail format input: The string returned by the str input: If you return true through validation, false/function Isemail (str) {var myreg =/^[-_a -za-z0-9]+@ ([_a-za-z0-9]+\.) 
+[a-za-z0-9]{2,3}$/; 
if (Myreg.test (str)) return true; 
return false; /* Purpose: Check that the input string is in line with the amount format is defined as a positive number with a decimal point, with a maximum of three digits after the decimal: s: string return: false/function Ismoney (s) {var Regu = "^[0-9]+[\." [0-9] 
{0,3}$ "; 
var re = new RegExp (Regu); 
if (Re.test (s)) {return true; 
else {return false; }/* Purpose: Check the lossEnter whether the string consists of English letters and numbers and underscores input: s: string return: false/function Isnumberor_letter (s) {//To determine whether it is a number or letter Var Regu if you return true through validation 
= "^[0-9a-za-z\_]+$"; 
var re = new RegExp (Regu); 
if (Re.test (s)) {return true; 
}else{return false; Use: Check that the input string is composed of English letters and numbers only.: s: String return: false/function Isnumberorletter (s) {//Judge whether it is a number or word if validation returns true 
female var Regu = "^[0-9a-za-z]+$"; 
var re = new RegExp (Regu); 
if (Re.test (s)) {return true; 
}else{return false; Use: Check that the input string is made up of Chinese characters, letters, and Numbers: value: String return: false/function Ischinaornumborlett (s) {// 
Whether is the Chinese character, the letter, the numeral composition var Regu = "^[0-9a-za-z\u4e00-\u9fa5]+$"; 
var re = new RegExp (Regu); 
if (Re.test (s)) {return true; 
}else{return false; }/* Purpose: To determine whether the date entered: Date: date; FMT: Date format return: false/function isDate (date, FMT) {if (fmt==null) f by validation returns true 
Mt= "YYYYMMDD"; 
var yindex = fmt.indexof ("yyyy"); 
if (yindex==-1) return false; 
var year = date.substring (yindex,yindex+4); var mindex = fmt.iNdexof ("MM"); 
if (mindex==-1) return false; 
var month = date.substring (mindex,mindex+2); 
var dindex = Fmt.indexof ("dd"); 
if (dindex==-1) return false; 
var day = date.substring (dindex,dindex+2); if (!isnumber) | | Year> "2100" | | 
year< "1900") return false; if (!isnumber (month) | | Month> "12" | | 
month< "") return false; 
if (Day>getmaxday (year,month) | | | | day< ") return false; 
return true; } function Getmaxday (year,month) {if month==4| | month==6| | month==9| | 
month==11) return "30"; 
if (month==2) if (year%4==0&&year%100!=0 | | year%400==0) return "29"; 
else return "28"; 
return "31"; }/* Purpose: Character 1 is the string 2 end Input: str1: string; str2: The contained string returns: false/Function Islastmatch (STR1,STR2) {var i, if returned by validation 
Ndex = Str1.lastindexof (STR2); 
if (str1.length==index+str2.length) return true; 
return false; }/* Purpose: Character 1 is entered as a String 2: str1: string; str2: The contained string returns: false/Function Isfirstmatch (STR1,STR2) {var) if validation returns True 
index = Str1.indexof (STR2); if (Index==0) return true; 
return false;  /* Purpose: Character 1 is a containing string 2 input: str1: string; str2: The contained string returns: false/Function IsMatch (STR1,STR2) {var index = If passed by validation returns true 
Str1.indexof (STR2); 
if (index==-1) return false; 
return true; /* Purpose: Check that the starting and ending date of the input is correct, the rule is two dates, the format is correct, and end as scheduled >= start date input: StartDate: Start date, string EndDate: End as expected, string return: Returns the FA if the validation returns true 
LSE/function Checktwodate (startdate,enddate) {if (!isdate (startdate)) {alert ("Incorrect start date!"); 
return false; 
else if (!isdate (EndDate)) {alert ("Incorrect end date!"); 
return false; 
else if (StartDate > EndDate) {alert ("Start date cannot be greater than End date!"); 
return false; 
return true; /* Purpose: Check the input Email box format is entered correctly: Stremail: string return: If you return true through validation, false/function Checkemail (stremail) {//var E Mailreg =/^[_a-z0-9]+@ ([_a-z0-9]+\.) 
+[a-z0-9]{2,3}$/; var Emailreg =/^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ 
\w-]+) +$/; 
if (Emailreg.test (Stremail)) {return true; }else{alert ("The email address you entered is not in the correct format!") 
"); 
return false; Use: Check the input phone number format entered correctly: StrphoNE: string return: false/function Checkphone (strphone) {var Phoneregwitharea =/^[0][1-9]{2,3}-[0-9]{5 If return true through validation 
, 10}$/; 
var phoneregnoarea =/^[1-9]{1}[0-9]{5,8}$/; 
var prompt = "The phone number you entered is not correct!" 
if (Strphone.length > 9) {if (Phoneregwitharea.test (Strphone)) {return true; 
}else{alert (prompt); 
return false; 
}else{if (phoneregnoarea.test (Strphone)) {return true; 
}else{alert (prompt); 
return false; Usage: Check the number of selected check boxes enter: Checkboxid: String return: Returns the number selected in this check box/function Checkselect (CHECKBOXID) {var check 
= 0; 
var i=0; if (document.all (CHECKBOXID). length > 0) {for (i=0; I<document.all (CHECKBOXID). length; i++) {if document.all ( 
CHECKBOXID). Item (i). checked) {check + 1; 
}}else{if (document.all (CHECKBOXID). checked) check = 1; 
} return check; 
The function gettotalbytes (Varfield) {if (Varfield = null) return-1; 
var totalcount = 0; for (i = 0; i< varField.value.length; i++) {if (varField.value.charCodEAt (i) > 127) TotalCount + + 2; 
else totalcount++; 
return totalcount; 
function Getfirstselectedvalue (CHECKBOXID) {var value = null; 
var i=0; if (document.all (CHECKBOXID). length > 0) {for (i=0; I<document.all (CHECKBOXID). length; i++) {if document.all (ch 
ECKBOXID). Item (i). checked) {value = document.all (CHECKBOXID). Item (i). value; 
Break 
}} else {if (document.all (CHECKBOXID). Checked) value = document.all (CHECKBOXID). value; 
return value; 
function Getfirstselectedindex (CHECKBOXID) {var value =-2; 
var i=0; if (document.all (CHECKBOXID). length > 0) {for (i=0; I<document.all (CHECKBOXID). length; i++) {if document.all (c 
HECKBOXID). Item (i). checked) {value = i; 
Break 
}} else {if (document.all (CHECKBOXID). Checked) value =-1; 
return value; 
function SelectAll (checkboxid,status) {if (document.all (CHECKBOXID) = null) return; if (document.all (CHECKBOXID). length > 0) {for (i=0; I<document.all checkbOXID). length; 
i++) {document.all (CHECKBOXID). Item (i). checked = status; 
} else {document.all (CHECKBOXID). checked = status; 
} function Selectinverse (CHECKBOXID) {if document.all (CHECKBOXID) = null) return; if (document.all (CHECKBOXID). length > 0) {for (i=0; I<document.all (CHECKBOXID). length; i++) {document.all (chec 
KBOXID). Item (i). Checked =!document.all (CHECKBOXID). Item (i) checked; 
} else {document.all (CHECKBOXID). Checked =!document.all (CHECKBOXID). checked; 
} function Checkdate (value) {if (value== ') return true; 
if (value.length!=8 | |!isnumber (value)) return false; 
var year = value.substring (0,4); 
if (year> "2100" | | | | year< "1900") return false; 
var month = value.substring (4,6); 
if (month> "| | month<") return false; 
var day = value.substring (6,8); 
if (Day>getmaxday (year,month) | | | | day< ") return false; 
return true; /* Purpose: Check that the input start and end dates are correct, that the rule is in the correct or null format with two dates, and the ending date >= the starting Date input: StartDate: Start date, string EnddaTE: End Date, string return: false/function Checkperiod (startdate,enddate) {if (!checkdate (startdate)) {if passed by validation returns true 
Alert ("Incorrect start date!"); 
return false; 
else if (!checkdate (EndDate)) {alert ("Incorrect end date!"); 
return false; 
else if (StartDate > EndDate) {alert ("Start date cannot be greater than End date!"); 
return false; 
return true; /* Purpose: Check that the securities code is correctly entered: Seccode: Securities code return: If you return true through validation, false/function Checkseccode (seccode) {if (Seccode) is returned. 
Length!=6) {alert ("The Securities code length should be 6 digits"); 
return false; 
} if (!isnumber (Seccode)) {alert ("Securities code can contain only numbers"); 
return false; 
return true; 
/**************************************************** Function:ctrim (Sinputstring,itype) Description: A function of a string to space parameters:itype:1= Remove the space on the left side of the string 2= Remove the space on the left of the string 0= remove the left and right spaces of the string return value: A string that removes the space ********************************** 
/function Ctrim (sinputstring,itype) {var stmpstr = '; 
var i =-1; 
if (Itype = = 0 | | itype = = 1) {while (stmpstr = = ") {++i; Stmpstr = SinputstrinG.substr (i,1); 
} sinputstring = sinputstring.substring (i); 
} if (Itype = = 0 | | itype = 2) {stmpstr = '; 
i = sinputstring.length; 
while (stmpstr = = ") {i; 
Stmpstr = Sinputstring.substr (i,1); 
} sinputstring = Sinputstring.substring (0,i+1); 
return sinputstring; }

The above is small set to introduce the JavaScript commonly used in the verification of Reg, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.