Common JavaScript judgment functions (Regular Expression Version)

Source: Internet
Author: User

I picked a post titled common JavaScript functions from the Internet two days ago, which contains verification functions that are commonly used on the client, however, all the functions in it use the string Decomposition Method for judgment,ArticleAfter submission, many friends suggested that many functions can be replaced by regular expressions. I will change these functions to regular expressions for verification tonight. Please give me some advice if they are good or not.


// Use this expression to obtain the string length.
Function Regdatalength (fdata)
{
VaR Vallength = Fdata. length;
VaR Reg =   New Regexp ( " ^ [\ U0391-\ uffe5] $ " );
VaR Result =   0 ;
For (I = 0 ; I < Vallength; I ++ )
{
If (Reg. Test (fdata. charat (I )))
{
Result+ = 2;
}
Else
{
Result++;
}
}
Return Result;
}

// Use a regular expression to determine whether it is a 0-9 Arabic number
Function Regisdigit (fdata)
{
VaRReg= NewRegexp ("^ [0-9] $");
Return(Reg. Test (fdata ));
}

// Application expansion determines whether it is a value
Function Regisnumber (fdata)
{
VaRReg= NewRegexp ("^ [-]? [0-9] + [\.]? [0-9] + $");
ReturnReg. Test (fdata)
}
// Verify that email is correct
Function Regisemail (fdata)
{
VaRReg= NewRegexp ("^ [0-9a-za-z] + @ [0-9a-za-z] + [\.] {1} [0-9a-za-z] + [\.]? [0-9a-za-z] + $");
ReturnReg. Test (fdata );
}
// Determine whether the mobile phone number is correct
Function Regisphone (fdata)
{
VaR Reg =   /^ (\ + 86 ) ? ( 1 [ 0 - 9 ] {10} ) $ / ;
Return Reg. Test (fdata );
}

Write the basic verification code here. If the verification code is incorrect, or a friend has a better way to leave a message. Careful friends may find that I used/^ (\ + 86) in the last mobile phone number verification function )? (1 [0-9] {10}) $/instead of new Regexp ("^ (\ + 86 )? (1 [0-9] {10}) $ "); originally I wanted to use the latter, but when I use it, JavaScript will report an error" Incorrect quantizer ", but it is normal in mtracer, so I changed it to the above format, and the strange error is gone. I don't know what's going on. Haha.
Download mtracer

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.