电话号码字段(tel varchar类型)存储3个号码,并且是以分号结尾,类似:
0510-12323475,0781-26354589,021-25698746 (同时存储3个号码)
0521-25698745,, (有可能只存储1个电话号码)
0521-25698745,021-25698475, (有可能只存储2个电话号码)
现在用正则判断电话号码输入是否正常,注意电话区号。
RegTel=/^(\d{3,4}-\d{7,8}(-\d{1,4})?)?$/这是我写的,但是不行。
------解决方案--------------------
- PHP code
$str = '0510-12323475,,';$pattern = '/,?\d{4}-\d+,?/';var_dump(preg_match($pattern, $str));
------解决方案--------------------
- PHP code
$ar = array( '0510-12323475,0781-26354589,021-25698746', '0521-25698745,,', '0521-25698745,021-25698475', '0317-88641,ttttttt',);$pattern = '/(?:\d{3,}-\d+,|,){3}/';foreach($ar as $v) { $v = trim($v) . str_repeat(',', 3-substr_count($v, ','));//补齐逗号 echo $v . ' ' . preg_match($pattern, $v) , PHP_EOL;}
------解决方案--------------------