JS Common 2: commonly used functions page 1/3

Source: Internet
Author: User

/*
-------------------------------------------------------------------------------
File Name: check. js
Note: JavaScript scripts are used to check the input data of the webpage submission form.
Version: 1.0
*/
/*
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;
}

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.