Jump to content

Form for numbers only


egturnkey

Recommended Posts

Hello dear friends,

 

i'm always thinking how to make an input text with numbers only and doesn't accept letters

 

case number two how to make it with certain number of digits, i mean only 6 numbers allowed to enter if less , it gives an error

 

<input type="text" name="name" value="" size="40" maxsize="6">

 

how can i make it

1- accept only numbers

2- accept only 6 digits if less shows error

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/195512-form-for-numbers-only/
Share on other sites

you can't make it only accept numbers in the field itself with just php alone, you'd have to use something like javascript.

 

but, you can just validate the entered data on the page that your form posts to.

 

ctype_digit() returns TRUE if the string is all numbers.  spaces don't count so you'd have to do a ctype_digit(trim($string)); just to play it safe and to be more user friendly

 

so basically on the page it posts to, have some code sort of like this:

 

if ( ( ctype_digit(trim($_POST['Number'])) ) && ( strlen(trim($_POST['Number'])) <= 6 ) )
{
    //correct
}
else
{
    $_SESSION['Error'] = 'Must be a number 6 digits or fewer.';
    header("Location: LastPage.php");
}

 

then on your form page do this:

 

if ( $_SESSION['Error'] != '' )
{
   echo $_SESSION['Error'];
   unset($_SESSION['Error']);
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.