在php中验证数字时出错

时间:2014-11-26 04:40:02

标签: php regex

我试图仅验证php中的数字,但它显示错误消息。我希望用户输入有效的手机号码,批次只输入4个号码。不应输入任何字符。请告诉我代码中的错误。 这是代码

 if (isset($_POST['submit'])) {
    $error = "";

    if (!empty($_POST['name'])) {
    $name = $_POST['name'];
    } else {
    $error .= "You didn't type in your name. <br />";
    }

    if (!empty($_POST['email'])) {
    $email = $_POST['email'];
      if (!preg_match("/^[_a-z0-9]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)){ 
      $error .= "The e-mail address you entered is not valid. <br/>";
      }
    } else {
    $error .= "You didn't type in an e-mail address. <br />";
    }
if (!empty($_POST['batch'])) {
    $batch = $_POST['batch'];
    } 
    if (!preg_match('/^[0-9]+$/', $batch)) { 
      $error .= "Enter a Valid Number. <br/>";
      }

    else {
    $error .= "You didn't type batch. <br />";
    }
     if(($_POST['code']) == $_SESSION['code']) { 
    $code = $_POST['code'];
    } else { 
    $error .= "The captcha code you entered does not match. Please try again. <br />";    
    }



    if (!empty($_POST['mobile'])) {
    $mobile = $_POST['mobile'];
    } 
    if (!preg_match('/^[0-9]+$/', $mobile)){ 
      $error .= "Enter A Valid Number. <br/>";
      }
    else {
    $error .= "You didn't type your Mobile Number. <br />";
    }

1 个答案:

答案 0 :(得分:0)

(!preg_match('/^[0-9]{4}$/', $batch)

如果您只想验证4个号码,请使用此选项。

相关问题