JS Verification Form Daquan

Source: Internet
Author: User
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 the 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 if the input string is empty or all are spaces
Input: str
Return:
Returns true if all is null, or False
*/
function IsNull (str) {
if (str = = "") return true;
var Regu = "^[]+$";
var re = new RegExp (Regu);
return Re.test (str);
}



/*
Purpose: Check whether the value of the input object conforms to the integer format
Input: str input string
Return: False if return true through validation


*/
function Isinteger (str) {
var regu =/^[-]{0,1}[0-9]{1,}$/;
return Regu.test (str);
}


/*
Purpose: Check whether the input mobile phone number is correct
Input:
S: string
Return:
Returns False if the validation returns true


*/
function Checkmobile (s) {
var regu =/^[1][3][0-9]{9}$/;
var re = new RegExp (Regu);
if (Re.test (s)) {
return true;
}else{
return false;
}
}



/*
Purpose: Check if the input string conforms to the positive integer format
Input:
S: string
Return:
Returns False if the validation returns true


*/
function Isnumber (s) {
var Regu = "^[0-9]+$";
var re = new RegExp (Regu);
if (S.search (re)!=-1) {
return true;
} else {
return false;
}
}


/*
Purpose: Check if the input string is a decimal number format, can be negative
Input:
S: string
Return:
Returns False if the validation returns true


*/
function Isdecimal (str) {
if (Isinteger (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;
}
}


/*
Purpose: Check if the value of the input object conforms to the port number format
Input: str input string
Return: False if return true through validation


*/
function Isport (str) {
Return (Isnumber (str) && str<65536);
}


/*
Purpose: Check if the value of the input object conforms to the e-mail format
Input: str input string
Return: False if return true through validation


*/
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 whether the input string conforms to the amount format
The format is defined as a positive number with decimals, with a maximum of three digits after the decimal point
Input:
S: string
Return:
Returns False if the validation returns true


*/
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 if the input string is composed of English letters and numbers and underscores only
Input:
S: string
Return:
Returns False if the validation returns true


*/
function Isnumberor_letter (s) {//Judge whether it is a number or letter


var Regu = "^[0-9a-za-z\_]+$";
var re = new RegExp (Regu);
if (Re.test (s)) {
return true;
}else{
return false;
}
}
/*
Purpose: Check if the input string is made up of English letters and numbers only
Input:
S: string
Return:
Returns False if the validation returns true


*/
function Isnumberorletter (s) {//Judge whether it is a number or letter


var Regu = "^[0-9a-za-z]+$";
var re = new RegExp (Regu);
if (Re.test (s)) {
return true;
}else{
return false;
}
}
/*
Purpose: Check if the input string is composed of Chinese characters, letters and numbers only
Input:
Value: String
Return:
Returns False if the validation returns true


*/
function Ischinaornumborlett (s) {//Judge whether it is Chinese characters, letters, numbers


var Regu = "^[0-9a-za-z\u4e00-\u9fa5]+$";
var re = new RegExp (Regu);
if (Re.test (s)) {
return true;
}else{
return false;
}
}


/*
Use: Determine whether the date
Enter: Date: dates; FMT: date format
Return: False if return true through validation
*/
function isDate (date, FMT) {
if (fmt==null) fmt= "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";
}


/*
Usage: Character 1 ends with string 2
Input: str1: string; str2: contained string
Return: False if return true through validation


*/
function Islastmatch (STR1,STR2)
{
var index = str1.lastindexof (STR2);
if (str1.length==index+str2.length) return true;
return false;
}



/*
Usage: Character 1 starts with string 2
Input: str1: string; str2: contained string
Return: False if return true through validation


*/
function Isfirstmatch (STR1,STR2)
{
var index = str1.indexof (STR2);
if (index==0) return true;
return false;
}


/*
Purpose: Character 1 is the containing string 2
Input: str1: string; str2: contained string
Return: False if return true through validation


*/
function IsMatch (STR1,STR2)
{
var index = str1.indexof (STR2);
if (index==-1) return false;
return true;
}



/*
Purpose: Check that the input start and end date is correct, the rule is two date of the correct format,
And end the scheduled >= start date
Input:
StartDate: Start Date, string
EndDate: End as scheduled, string
Return:
Returns False if the validation returns true


*/
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 format of email box entered correctly
Input:
Stremail: String
Return:
Returns False if the validation returns true


*/
function Checkemail (stremail) {
var emailreg =/^[_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;
}
}


/*
Purpose: Check the format of the phone number you entered correctly
Input:
Strphone: String
Return:
Returns False if the validation returns true


*/
function Checkphone (strphone) {
var phoneregwitharea =/^[0][1-9]{2,3}-[0-9]{5,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;
}



}
}



/*
Purpose: Check the number of check boxes selected
Input:
CHECKBOXID: String
Return:
Returns the number of 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;
}


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 (CHECKBOXID). 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 (CHECKBOXID). 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 (CHECKBOXID). 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 date entered is correct, the rule is two dates are in the correct format or are empty
and end Date >= start date
Input:
StartDate: Start Date, string
EndDate: End Date, string
Return:
Returns False if the validation returns true


*/
function Checkperiod (startdate,enddate) {
if (!checkdate (StartDate)) {
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 the security code is correct
Input:
Seccode: Securities Code
Return:
Returns False if the validation returns true


*/
function Checkseccode (seccode) {
if (Seccode.length!=6) {
Alert ("The length of the securities code should be 6 digits");
return false;
}


if (!isnumber (Seccode)) {
Alert ("The 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 side of the string
0= Remove the space on the left and right side of the string
Return value: A string that removes a 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;
}




Detailed Source reference: http://www.jb51.net/article/4976.htm

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.