Added verification for mobile phone numbers starting with 158 and 159.
Copy codeThe Code is as follows:
// Remove spaces at the beginning and end of the string
String. prototype. Trim = function (){
Var m = this. match (/^ \ s * (\ S + (\ s + \ S +) *) \ s * $ /);
Return (m = null )? "": M [1];
}
// Verify the valid mobile phone number
String. prototype. isMobile = function (){
Return (/^ (? : 13 \ d | 15 [89])-? \ D {5} (\ d {3} | \ * {3}) $/. test (this. Trim ()));
}
Example:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <ptml xmlns = "http://www.w3.org/1999/xhtml" lang = "gb2312"> <pead> <title> instance: verify the mobile phone number </title> <meta http-equiv = "content-type" content = "text/html; charset = gb2312 "/> </pead> <body> </ptml>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]