表单验证中以 2 位数字开头的数字


number that start with 2 digits in form validation

>我尝试用减法来"拾取"2位数字,但它不起作用,"牛肝菌"必须以"20"开头,否则会显示消息"介绍独奏10位数字de tu Boleta!

谢谢!

<?php
// define variables and set to empty values
$nombreErr = $boletaErr = "";
$nombre = $boleta =  "";
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (empty($_POST["boleta"])){
$boletaErr = "Boleta is required";
}
else{
$boleta = test_input($_POST["boleta"]);
$boletavalida=substr($boleta,0,2); 
//check if boleta only contains numbers
if (!preg_match('/^[0-9]{10}+$/', $boleta) && !$boletavalida ==20){
$boletaErr="Introduce solo los 10 digitos de tu Boleta!";
}   
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

以下行不正确:

if (!preg_match('/^[0-9]{10}+$/', $boleta) && !$boletavalida == 20) {

您否定了$boletavalida的值,而实际上您的意思是"不等于",应该使用 != 运算符。

请尝试以下操作:

if (!preg_match('/^[0-9]{10}+$/', $boleta) && $boletavalida != '20') {

更改以下内容:

if (!preg_match('/^[0-9]{10}+$/', $boleta) && !$boletavalida ==20){

对此:

if ((!preg_match('/^[0-9]{10}+$/', $boleta) && (strpos($boletavalida, '20') != 0)){