Javascript Verification Method

Source: Internet
Author: User
Tags character classes

1. digit Verification
An integer of 1.1
/^ (-| \ + )? \ D + $/. Test (STR)
An integer greater than 0 in 1.2 (used for verification of sent IDs)
/^ \ D + $/. Test (STR)
Verification of a negative integer of 1.3
/^-\ D + $/. Test (STR)
2. Time
2.1 short time, such as (13:04:06)
Function Istime (STR)
{
VaR A = Str. Match (/^ (\ D {1, 2 })(:)? (\ D {1, 2}) \ 2 (\ D {1, 2}) $ /);
If (A = NULL) {alert ('input parameter is not in the timeformat '); Return false ;}
If (A [1]> 24 | A [3]> 60 | A [4]> 60)
{
Alert ("Time Format incorrect ");
Return false
}
Return true;
}
2.2 Short date, such)
Function strdatetime (STR)
{
VaR r = Str. match (/^ (\ D {}) (-| \/) (\ D {}) \ 2 (\ D {}) $ /);
If (r = NULL) return false;
VaR d = new date (R [1], R [3]-1, R [4]);
Return (D. getfullyear () = R [1] & (D. getmonth () + 1) = R [3] & D. getdate () = R [4]);
}
2.3 long time, for example (13:04:06)
Function strdatetime (STR)
{
VaR Reg =/^ (\ D {}) (-| \/) (\ D {}) \ 2 (\ D }) (\ D {1, 2}) :( \ D {1, 2}) :( \ D {1, 2}) $ /;
VaR r = Str. Match (REG );
If (r = NULL) return false;
VaR d = new date (R [1], R [3]-1, R [4], R [5], R [6], R [7]);
Return (D. getfullyear () = R [1] & (D. getmonth () + 1) = R [3] & D. getdate () = R [4] & D. gethours () = R [5] & D. getminutes () = R [6] & D. getseconds () = R [7]);
}
2.4 is only for years and months. Such as (2003-05, or 2003-5)
2.5 only hours and minutes, such)
3. Form class
3.1 The value of all forms cannot be blank
<Input onblur = "If (this. value. replace (/^ \ s + | \ s + $/g, '') ='') Alert ('cannot be blank! ') ">
The value of the 3.2 multi-row text box cannot be blank.
The value of the 3.3 Multi-line text box cannot exceed smaxstrleng
The value of the 3.4 multi-row text box cannot be less than that of smixstrleng
3.5 determine whether a single shard is selected.
3.6 check whether the check box is selected.
3.7 select all check boxes, select multiple, do not select all, select Inverse
3.8 determine the file type during File Upload
4. character classes
4.1 characters are all composed of letters A-Z or a A-Z
<Input onblur = "If (/[^ A-Za-Z]/g. Test (this. Value) Alert ('error')">
The 4.2 character is composed of letters and numbers.
<Input onblur = "If (/[^ 0-9a-za-z]/g. Test (this. Value) Alert ('error')">
The 4.3 judgment character consists of letters, numbers, underscores, and periods. It must start with an underscore or letter.
/^ ([A-Za-Z _] {1}) ([\ W] *) $/g. Test (STR)
4.4 string replacement function. Replace ();
5. Browser
5.1 determine the browser type
Window. Navigator. appname
5.2 judge the IE version
Window. Navigator. appversion
5.3 determine the client resolution
Window. Screen. height; window. Screen. width;

6. Integration class
6.1 email judgment.
Function Ismail (Mail)
{
Return (New Regexp (/^ \ W + (-\ W +) | (\. \ W +) * \ @ [A-Za-z0-9] + ((\. |-) [A-Za-z0-9] + )*\. [A-Za-z0-9] + $ /). test (Mail ));
}
6.2 mobile phone number verification
6.3 ID card verification
Function isidcardno (Num)
{
If (isnan (Num) {alert ("the input is not a number! "); Return false ;}
VaR Len = num. length, RE;
If (LEN = 15)
Re = new Regexp (/^ (\ D {6 })()? (\ D {2}) (\ D {2}) (\ D {2}) (\ D {3}) $ /);
Else if (LEN = 18)
Re = new Regexp (/^ (\ D {6 })()? (\ D {4}) (\ D {2}) (\ D {2}) (\ D {3}) (\ D) $ /);
Else {alert ("the number of digits entered is incorrect! "); Return false ;}
VaR A = num. Match (re );
If (! = NULL)
{
If (LEN = 15)
{
VaR d = new date ("19" + A [3] + "/" + A [4] + "/" + A [5]);
VaR B = D. getyear () = A [3] & (D. getmonth () + 1) = A [4] & D. getdate () = A [5];
}
Else
{
VaR d = new date (A [3] + "/" + A [4] + "/" + A [5]);
VaR B = D. getfullyear () = A [3] & (D. getmonth () + 1) = A [4] & D. getdate () = A [5];
}
If (! B) {alert ("entered ID card number" + A [0] + "contains incorrect birthdate! "); Return false ;}
}
Return true;
}

6.4)

Obtains the number of bytes of the input string, which is useful for input verification. If the field in the database is varchar (100), you can only enter 100 English letters, numbers, or 50 Chinese characters. Generally, the length of the input box cannot exceed 50, that is, the input can contain up to 50 Chinese or English characters. This method can determine the length of a byte for precise control.

Function getbytescount (STR)
{
If (STR = NULL)
{
Return 0;
}
Else
{
Return (Str. Length + Str. Replace (/[\ u0000-\ u00ff]/g, ""). Length );
}
}

6.4.1

Function getbytescount (STR, maxlength)
{
If (STR = NULL)
{
Return 0;
}
Else
{

VaR factlength = Str. Length + Str. Replace (/[\ u0000-\ u00ff]/g, ""). length;
If (factlengtn <maxlength)

{

Return STR;

}

Else

{

Return Str. substring (0, maxlength );

}
}
}

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.