egturnkey Posted March 16, 2010 Share Posted March 16, 2010 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 More sharing options...
5kyy8lu3 Posted March 17, 2010 Share Posted March 17, 2010 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']); } Link to comment https://forums.phpfreaks.com/topic/195512-form-for-numbers-only/#findComment-1027368 Share on other sites More sharing options...
AdRock Posted March 17, 2010 Share Posted March 17, 2010 Here is a tutorial which may help you http://www.plus2net.com/php_tutorial/php_form_validation.php Link to comment https://forums.phpfreaks.com/topic/195512-form-for-numbers-only/#findComment-1027369 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.