Common JavaScript detection scripts (2)

Source: Internet
Author: User
/* *
* Purpose: Check whether the input string is composed of English letters, numbers, and underscores (_).
* Input: S, string
* Return: If the verification succeeds, true is returned; otherwise, false is returned.
  */
Function Isnumberor_letter (s ){
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: If the verification succeeds, true is returned; otherwise, false is returned.
  */
Function Isnumberorletter (s ){ // Determine 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: If the verification succeeds, true is returned; otherwise, false is returned.
  */
Function Ischinaornumborlett (s ){
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: 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 ;
}
 
/* *
* Purpose: Obtain the date value of the last day of each month.
* Input: Date: Year; month: Month
* Return: If the verification succeeds, true is returned; otherwise, false is returned.
  */
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: determines 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: determines 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 and end as scheduled> = Start Date
* Input: startdate: Start Date (string), enddate: end date as scheduled (string)
* Return: 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 ;
}

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.