node-mobile
Version:
Mobile number generator and tester.
57 lines (54 loc) • 1.29 kB
JavaScript
"use strict";
exports.MOBILE_NUMBER_LOOSE = /^[1]([0-9])[0-9]{9}$/;
exports.MOBILE_NUMBER_STRICT = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
/**
* 检测一个字符串是否为手机号
*
* @param {String} value 待检测的值
* @param {Boolean} strict 是否严格模式检测
*
* 默认为严格模式,检测正则表达式为:
*
* ```js
* /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/
* ```
*
* 已支持的号段有:
*
* - 中国移动:
* - 134 135 136 137 138 139
* - 182 183 184 187 188
* - 150 151 152 157 158 159
* - 165
* - 172 178(4G)
* - 147(上网卡)
* - 1440(物联网) 148(物联网)
* - 198
* - 中国联通
* - 130 131 132
* - 155 156
* - 185 186
* - 171 175 176(4G)
* - 145(上网卡)
* - 146(物联网)
* - 166
* - 中国电信
* - 133 149 153
* - 180 181 189 191
* - 173 174 177(4G)
* - 1410(物联网)
* - 199
* - 虚拟运营商
* - 170 171
*
* 普通模式正则表达式为:
*
* ```js
* /^[1]([3-9])[0-9]{9}$/
* ```
*/
exports.isMobile = function(mobile, strict) {
return strict === true
? MOBILE_NUMBER_STRICT.test(mobile)
: MOBILE_NUMBER_LOOSE.test(mobile);
};