Js verification form Overview

Source: Internet
Author: User

Good JS verification ~~~~~~~~~~~~~~~~~~~~~~~~~
Purpose: Verify the IP address format.
Input: strIP: IP Address
Return: If the verification succeeds, true is returned; otherwise, false is returned;

*/
Function isIP (strIP ){
If (isNull (strIP) return false;
Var re =/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/g // Regular Expression matching IP addresses
If (re. test (strIP ))
{
If (RegExp. $1 <256 & RegExp. $2 <256 & RegExp. $3 <256 & RegExp. $4 <256) return true;
}
Return false;
}

/*
Purpose: Check whether the input string is null or all are spaces.
Input: str
Return Value:
If all values are null, true is returned. Otherwise, false is returned.
*/
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 string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
Function isInteger (str ){
Var regu =/^ [-] {0, 1} [0-9] {1,} $ /;
Return regu. test (str );
}

/*
Purpose: Check whether the entered mobile phone number is correct.
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
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 whether the input string conforms to the positive integer format.
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function isNumber (s ){
Var regu = "^ [0-9] + $ ";
Var re = new RegExp (regu );
If (s. search (re )! =-1 ){
Return true;
} Else {
Return false;
}
}

/*
Purpose: Check whether the input string is in decimal number format. It can be a negative number.
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
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 whether the value of the input object conforms to the port number format.
Input: str string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
Function isPort (str ){
Return (isNumber (str) & str <65536 );
}

/*
Purpose: Check whether the value of the input object complies with the E-Mail format.
Input: str string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
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 meets the amount format
The format is defined as a positive number with decimal places. A maximum of three decimal places can be entered.
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
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 whether the input string is composed of English letters, numbers, and underscores (_).
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function isNumberOr_Letter (s) {// determines 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 whether the input string consists of only English letters and numbers.
Input:
S: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function isNumberOrLetter (s) {// determines 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 whether the input string consists of only Chinese characters, letters, and numbers.
Input:
Value: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function isChinaOrNumbOrLett (s) {// determines whether it is composed of Chinese characters, letters, and numbers.

Var regu = "^ [0-9a-zA-Z \ u4e00-\ u9fa5] + $ ";
Var re = new RegExp (regu );
If (re. test (s )){
Return true;
} Else {
Return false;
}
}

/*
Purpose: determine whether it is a date.
Input: date; fmt: date Format
Return: If the verification succeeds, true is returned; otherwise, false is returned.
*/
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) | year> "2100" | year <"1900") return false;
If (! IsNumber (month) | month> "12" | month <"01") return false;
If (day> getMaxDay (year, month) | day <"01") 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 & amp; year % 100! = 0 | year % 400 = 0)
Return "29 ";
Else
Return "28 ";
Return "31 ";
}

/*
Purpose: whether character 1 ends with string 2
Input: str1: string; str2: contained string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
Function isLastMatch (str1, str2)
{
Var index = str1.lastIndexOf (str2 );
If (str1.length = index + str2.length) return true;
Return false;
}


/*
Purpose: whether character 1 starts with string 2
Input: str1: string; str2: contained string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
Function isFirstMatch (str1, str2)
{
Var index = str1.indexOf (str2 );
If (index = 0) return true;
Return false;
}

/*
Purpose: character 1 is a string containing 2
Input: str1: string; str2: contained string
Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/
Function isMatch (str1, str2)
{
Var index = str1.indexOf (str2 );
If (index =-1) return false;
Return true;
}


/*
Purpose: Check whether the input start and end dates are correct. The rules are in the correct format for the two dates,
End on schedule> = Start Date
Input:
StartDate: start date, string
EndDate: end date, string
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function checkTwoDate (startDate, endDate ){
If (! IsDate (startDate )){
Alert ("Incorrect start date! ");
Return false;
} Else if (! IsDate (endDate )){
Alert ("the end date is incorrect! ");
Return false;
} Else if (startDate> endDate ){
Alert ("the start date cannot be later than the end date! ");
Return false;
}
Return true;
}

/*
Purpose: Check whether the entered Email address format is correct.
Input:
StrEmail: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
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 format you entered is incorrect! ");
Return false;
}
}

/*
Purpose: Check whether the entered phone number format is correct.
Input:
StrPhone: String
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
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 incorrect! "
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 selected check boxes.
Input:
CheckboxID: String
Return Value:
Returns the number of selected items in the 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) & gt; 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> "12" | month <"01") return false;

Var day = value. substring (6, 8 );
If (day> getMaxDay (year, month) | day <"01") return false;

Return true;
}

/*
Purpose: Check whether the input start and end dates are correct. The rules are correct or empty.
And end date> = Start Date
Input:
StartDate: start date, string
EndDate: end date, string
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function checkPeriod (startDate, endDate ){
If (! CheckDate (startDate )){
Alert ("Incorrect start date! ");
Return false;
} Else if (! CheckDate (endDate )){
Alert ("the end date is incorrect! ");
Return false;
} Else if (startDate> endDate ){
Alert ("the start date cannot be later than the end date! ");
Return false;
}
Return true;
}

/*
Purpose: Check whether the securities code is correct
Input:
SecCode: securities code
Return Value:
If the verification succeeds, true is returned. Otherwise, false is returned.

*/
Function checkSecCode (secCode ){
If (secCode. length! = 6 ){
Alert ("the length of the securities code should be 6 characters ");
Return false;
}

If (! IsNumber (secCode )){
Alert ("securities code can only contain numbers ");


Return false;
}
Return true;
}

/*************************************** *************
Function: cTrim (sInputString, iType)
Description: String space-removing Function
Parameters: iType: 1 = remove the space on the left of the string

2 = remove the space on the left of the string
0 = remove spaces on the left and right of the string
Return value: a string that removes spaces.
**************************************** ************/
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;
}

/*

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.