Check whether html form numeric field has a maximum of 6 numbers with PHP

Asked

Viewed 690 times

5

I need to test if field is filled and if it is composed of up to 6 numbers using the PHP language

  • I could describe better where you’re struggling?

  • Sure, I have 2 fields in a form and one of them is numero_de_record,

  • this number may have a maximum of 6 characters

  • I need to use PHP to validate, check, if the field has these 6 numbers or not , if yes, it sends the data, otherwise it returns to the index

  • Do you really need to validate an HTML form in PHP? The ideal would be in javascript, because the validation can be done during the filling, and in PHP the validation would only be done after Submit, something not very optimized for the user.

  • is for a task, so the need in PHP, otherwise would have already done in JS

  • 3

    @Rogériodec should always do the server-side check, more important than the client-side check. Ideally it is in both, but in javascript is only more user friendly, because it gives no security at all

Show 2 more comments

3 answers

7

You can check if a field is filled in several ways:

if($campo != NULL )
if(!$campo)
if(!$campo == "")
if(isset($campo))
if(!empty($campo))
if(strlen($campo) > 0) 
if(!strlen($campo)) // porque esta função retorna zero caso o $campo esteja vazio

The !serves to deny an operation, "evaluates" the inverse of the following.

To check whether the $campo contains at most 6 numbers you can use strlen as before:

if(strlen($campo)<=6 && strlen($campo))

Now you must use what fits your needs best.

References:
NULL
isset
Empty
strlen

  • If there is something wrong with my answer other than -1 please comment, no one is perfect, we are all here to learn and share knowledge.

  • Yes, I commented here a solution, and has gnt that signals with negatively, will understand.

3

In addition to the other answers, which are correct. I believe you want to evaluate whether the field is filled up 6 numbers. I created two options:

Option 1

You can use the is_numeric:

It will return true if the string is a numeric string, and along with the strlen of the other answers you will get what you want:

$numeros = trim($_POST['numero_de_registro']);

if(is_numeric($numeros) && $numeros != "" && strlen($numeros) <= 6){
    echo "Está tudo certo aqui -> ".$numeros;
}

Examples of return from is_numeric:

"123a" -> false
"123"  -> true
" 123" -> true
"1a2"  -> false

Option 2

Using the preg_match

The preg_match will return 1 if the evaluation is correct. That is, if there are 6 digits in this string:

$numeros = trim($_POST['numero_de_registro']);
if(preg_match("/\d{1,6}/", $numeros)){
    echo "Está tudo certo aqui -> ".$numeros;
}

Understanding the acronyms:

\d    -> dígitos [0-9]
{1,6} -> quantidade dígitos que devem existir nessa avaliação, no mínimo 1 e no máximo 6

If you don’t want the 0, you can change for this:

preg_match("/[1-9]{1,6}/", $numeros);

2

Using php you use the function strlen(), that counts the number of characters in the string:

$str=strlen($_POST['sua_variável']);
if($str==6){echo 'ok';}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.