php代码来验证文本框值


php code to validate textbox values

我的表单中有两列。.

文本框1

用户在我的文本框中输入数据,如

textbox1=501502,

总计=2

我需要验证码。。以验证文本框1。

用于EX

如果用户输入

total=2,则文本框1只接受两个值,如低于

textbox1=501502,

如果用户尝试输入两个以上的值,则需要回显消息。。。

如果我在总文本框中输入1,那么它允许用户在文本框中只输入1个值1,不超过1…

下面是我的代码。。。

我不知道如何验证这种情况。。。。plz

$textbox1 = $_POST['textbox1']; 
$arr = explode(",", rtrim($textbox1 , ', '));
$total= $_POST['total'];


 public function receipt_exist($arr,$total)
        {
            $errors=array();
            //validation code here
             return $errors;        
        }

如果您只需要检查数组的长度,那就足够简单了。也许是这样的:

$textbox1 = $_POST['textbox1']; 
$arr = explode(",", rtrim($textbox1 , ', '));
$total= intval($_POST['total']);
if (count($arr) != $total) {
    // display error message
} else {
    // continue
}

错误消息由您决定。例如,您可能会发出一些HTML并结束响应。(如果你结束响应,那么你就不需要else块,如果没有它,代码看起来会更干净。相反,你只需要在if块之后继续逻辑。)

或使用javascript制作。。

<input type="text" id="total" name="total">  
<input type="text" id="textbox1" name="textbox1" onkeypress="return checkInput()">
<script>
function checkInput(){   
        var total = document.getElementById('total').value;
        var textbox1 = document.getElementById('textbox1').value;
        if(total.length*3 < textbox1.length){
             alert("entered value is bigger");
             return false;
       }
}
</script>

FIDDLE

$textbox1 = $_POST['textbox1']; 
$arr = explode(",", rtrim($textbox1 , ', '));
$total= $_POST['total'];
public function receipt_exist($arr,$total){
  $errors=array();
  if(count($arr)>$total){
   $errors[] = "ERRROR";
  }   
return $errors;        
}