亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在php中為具有固定范圍的數字preg_match?

如何在php中為具有固定范圍的數字preg_match?

PHP
泛舟湖上清波郎朗 2022-09-12 10:07:27
如何在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


查看完整回答
反對 回復 2022-09-12
  • 1 回答
  • 0 關注
  • 94 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號