Some JS Code for checking data

Source: Internet
Author: User

These JS codes are some of the code that is frequently used in Form submission. Most of the Code is also written using regular expressions!


// Remove the left space;
Function ltrim (s ){
Return s. replace (/^ \ s */,"");
}
// Remove the right space;
Function rtrim (s ){
Return s. replace (/\ s * $ /,"");
}
// Remove left and right spaces;
Function trim (s ){
Return rtrim (ltrim (s ));
}
// Whether it is null;
Function IsEmpty (_ str ){
Var tmp_str = trim (_ str );
Return tmp_str.length = 0;
}
// Valid Email;
Function IsMail (_ str ){
Var tmp_str = trim (_ str );
Var pattern =/^ [_ a-z0-9-] + (. [_ a-z0-9-] +) * @ [a-z0-9-] + (. [a-z0-9-] +) * $ /;
Return pattern. test (tmp_str );
}
// Valid number;
Function IsNumber (_ str ){
Var tmp_str = trim (_ str );
Var pattern =/^ [0-9]/;
Return pattern. test (tmp_str );
}
// Valid color value;
Function IsColor (color ){
Var temp = color;
If (temp = "") return true;
If (temp. length! = 7) return false;
Return (temp. search (// # [a-fA-F0-9] {6 }/)! =-1 );
}
// Valid link;
Function IsURL (url ){
Var sTemp;
Var B = true;
STemp = url. substring (0, 7 );
STemp = sTemp. toUpperCase ();
If (sTemp! = "HTTP: //") | (url. length <10 )){
B = false;
}
Return B;
}
// Indicates whether the mobile phone number is valid;
Function IsMobile (_ str ){
Var tmp_str = trim (_ str );
Var pattern =/13 \ d {9 }/;
Return pattern. test (tmp_str );
}

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.