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