How to validate inserted value in text field with php?

3

I have a project, which consists of pulling a code inserted by the user in the following field

<input type=text name=CODNOME><br>

by putting the following value 001 into an msg

<?php $CODNOME = '001' echo " testetes"; ?>

I know I'm lost in logic, could anyone guide me what to do or where to look?

    
asked by anonymous 16.03.2018 / 13:30

2 answers

3

Simple:

<?php 

if($_POST['CODNOME'] == "001"){
  $msg = "mensagem1";
} 

else if(($_POST['CODNOME'] == "002"){
  $msg = "mensagem2";
}

else {
  $msg = "Opção inválida";
}

echo $msg;
?>

You can also use switch :

<?php 

switch($_POST['CODNOME']){
  case "001"
  $msg = "mensagem1";
  break;

  case "002"
  $msg = "mensagem2";
  break;

  default:
  $msg = "Opção inválida";
  break;
} 
echo $msg;
?>
    
16.03.2018 / 14:04
1

To validate in PHP use the preg_match function, for example:

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

It returns 1 if it is valid, 0 if it is invalid or false if an error occurs

The first parameter of the function is the regex and the second the string, in its case the value sent by the user

Now you just need to create a regex to validate, to test regular expressions I recommend this site

    
16.03.2018 / 13:38