This time I will bring you a detailed explanation of the regular verification of mobile phone numbers (with code). What are the precautions for regular verification of mobile phone numbers? The following is a practical case, let's take a look.
Only numbers allowed
<xsl:attribute name="onkeyup">value=value.replace(/[^\d]/g,'')</xsl:attribute> <xsl:attribute name="onbeforepaste">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))</xsl:attribute>
//固定电话只要有一个填写即可 $('.form-inline').submit(function(){ var m = $('input[name=mobile]').val(); var p = $('input[name=phone]').val(); var reg = /^1\d{10}$/; if((m == "" || !reg.test(m)) && p == ""){ $('input[name=mobile]').addClass('error_color'); $('input[name=mobile]').tooltip('show'); return false; }else{ $('input[name=mobile]').removeClass('error_color'); $('input[name=mobile]').tooltip('hide'); return true; } })
Let’s take a look at the latest mobile phone number verificationRegular expression
Due to the continuous updating of mobile phone number segments, the previous regular expressions can no longer meet the needs. Rewrite this expression, the number segment data source is based on: http://www.jihaoba.com/tools/haoduan/Existing mobile phone number segment:
手机:139 138 137 136 135 134 147 150 151 152 157 158 159 178 182 183 184 187 188China Unicom: 13 0 131 132 155 156 185 186 145 176
Telecom: 133 153 177 173 180 181 189
Virtual operator:
170 171After sorting:
130~139 145,147 15[012356789 ] 178,176,177,173,170,171 180~189var regex = { mobile: /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/ }
js:
var bool = checkRegexp(jq("#mobile"), /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/, "电话格式不对");
Expression analysis:
"/" represents a regular expression Mode. "^" represents the starting position of thestring , and "$" represents the end position of the string.
"?" means matching one or zero of the previous characters, so 0? here means that the mobile phone number can start with 0 or not. The next part verifies the 11-digit mobile phone number, starting from 13, because it ranges from 130-139, so the optional range is [0-9], the number starting with 15 does not have 154, so there is no 154 in [] Without the number 4, of course it can also be written as [0-35-9]. The following numbers 18 and 14 are the same as above. The parentheses enclosed represent a subexpression, which contains 4 optional branches separated by "|". In the regular expression, the Analysis completed. What if a big-headed ghost writes something like 86,17951 on the front?/^(0|86|17951)?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/
C# version:
using System.Text.RegularExpressions; public static bool IsTelephone(string str_telephone) { return Regex.IsMatch(str_telephone, @"^(0|86|17951)?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$"); }
Detailed explanation of using regular expressions to extract strings (with code)
Using regular expressions in How to find letters and numbers in js
The above is the detailed content of Detailed explanation of regular verification of mobile phone number (with code). For more information, please follow other related articles on the PHP Chinese website!