2014-11-26 2 views
1

PHP에서 숫자의 유효성을 검사하려고하지만 오류 메시지가 표시됩니다. 사용자가 올바른 숫자의 휴대 전화 번호 만 입력하면 4 자리 만 입력됩니다. 문자를 입력 할 필요가 없습니다. 코드의 오류를 알려주십시오. 다음은 코드PHP에서 숫자의 유효성을 검사하는 동안 오류가 발생했습니다

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 />"; 
    } 
+2

'if (! preg_match ('/^[0-9] + $ /', $ batch)) {'로 변경하면 숫자를 입력 할 때마다 오류 메시지가 표시됩니다. – Cyclonecode

답변

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

사용을하다이 만 4 번호를 확인하려는 경우.

관련 문제