如何在php中為具有固定范圍的數字preg_match?我想在文本框中允許8位數字,而不以0開頭。例如12345678以下是我的一段代碼。if(isset($_POST['submit1'])) { if(empty($_POST["phone"])) { $error .= '<p><label class="text-danger">Please Enter your phone number</label></p>'; } else{ if(!preg_match("/^[1-9][0-9]{8}$/",$PhNum)) { $error .= '<p><label class="text-danger">Only 8 digit numbers are allowed</label></p>'; } }}謝謝。
1 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
您的模式只是稍微偏離了一點,您應該期望在初始數字(不是零)之后有七位數字:
if (!preg_match("/^[1-9][0-9]{7}$/", $PhNum)) {
$error .= '<p><label class="text-danger">Only 8 digit numbers are allowed</label></p>';
}
概念:
[1-9][0-9]{7} <-- seven digits, for a total of 8 digits
^^^ one digit
- 1 回答
- 0 關注
- 94 瀏覽
添加回答
舉報
0/150
提交
取消