PHP验证逗号serprated 10位电话号码

时间:2014-06-05 18:43:23

标签: php validation pattern-matching

我正试图从服务器端处理以检查字符串格式是否正确,例如

 $string = "9000010000,9100011000,9000020000      // Its correct pattern
 $string = "9000010000,9100011000,9000020000,   // Its WRONG   Reason comma in last place
 $string = ",9000010000,9100011000,9000020000,   // Its WRONG   Reason comma in first place
 $string = ",9000010000,9100011000,,9000020000,   // Its WRONG  Reason ,,
 $string = "9000010000,9100011,9000020000,   // Its WRONG because Middle no. is not 10 digit

我已经使用此代码通过JavaScript进行了客户端验证

    var listIsOk=/^(\d{10},)*\d{10}$/.test(contact_list);
if(listIsOk==true){
alert("success"
}
else {
alert("string is wrong")
}

我想知道如何在PHP上实现这一点,谢谢你提前

2 个答案:

答案 0 :(得分:1)

if (preg_match('/^(\d{10},)*\d{10}$/', $string)) {
  echo 'success';
} else {
  echo 'string is wrong';
}

答案 1 :(得分:0)

首先查看php函数explode(),它允许您将字符串拆分为一组电话号码。从那里你需要循环并查看每一个以确定它是否正确。