ed9uardo0
ed9uardo0

Reputation: 59

number that start with 2 digits in form validation

I tried with substract to 'pick up' the 2 digits but it does not work ,'boleta' have to start with '20',else is show the message "Introduce solo los 10 digitos de tu Boleta!".

thanks!

<?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;
}
?>

Upvotes: 0

Views: 53

Answers (2)

Aiias
Aiias

Reputation: 4748

This following line is incorrect:

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

You are negating the value of $boletavalida when instead, you really mean "is not equal to" which should be using the != operator.

Try the below:

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

Upvotes: 1

ek9
ek9

Reputation: 3442

Change this:

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

To this:

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

Upvotes: 0

Related Questions