iNko Posted November 3, 2012 Share Posted November 3, 2012 Yesterday i got help with checking if textfield is empty, now i also need to make it so only numbers can be inserted in a specific textfield. Heres the code that checks if field is empty: if (empty($_POST['numbers'])) { $error [] = 1; } else { $numbers= $_POST['numbers']; } Can i add another IF after the ELSE in this code that checks if its numbers? Quote Link to comment https://forums.phpfreaks.com/topic/270243-need-help-with-only-inserting-numbers-in-textfield/ Share on other sites More sharing options...
haku Posted November 3, 2012 Share Posted November 3, 2012 I'm guessing you want something like this: if (empty($_POST['numbers'])) { $error [] = 1; } elseif(!is_numeric($_POST['numbers']) { $error[] = 2; } else { $numbers= $_POST['numbers']; } Quote Link to comment https://forums.phpfreaks.com/topic/270243-need-help-with-only-inserting-numbers-in-textfield/#findComment-1389913 Share on other sites More sharing options...
50r Posted November 3, 2012 Share Posted November 3, 2012 most of the time i use this ctype_digit($_POST['numbers']) Quote Link to comment https://forums.phpfreaks.com/topic/270243-need-help-with-only-inserting-numbers-in-textfield/#findComment-1389917 Share on other sites More sharing options...
iNko Posted November 3, 2012 Author Share Posted November 3, 2012 (edited) I'm guessing you want something like this: if (empty($_POST['numbers'])) { $error [] = 1; } elseif(!is_numeric($_POST['numbers'])) { $error[] = 2; } else { $numbers= $_POST['numbers']; } Thank you, this works very well. Also as a side question, can i make this code without the 'elseif'? Something like this: if ((empty($_POST['numbers'])) OR (!is_numeric($_POST['numbers'])) { $error [] = 1; } else { $numbers= $_POST['numbers']; also 'is_numeric' is for numbers, is there a name for 'is letters'? Edited November 3, 2012 by iNko Quote Link to comment https://forums.phpfreaks.com/topic/270243-need-help-with-only-inserting-numbers-in-textfield/#findComment-1389919 Share on other sites More sharing options...
Pikachu2000 Posted November 3, 2012 Share Posted November 3, 2012 is_numeric() will allow values such as 0x57BEF4A and 1.99456e313. ctype_digit and ctype_alpha would be more appropriate for form data. Quote Link to comment https://forums.phpfreaks.com/topic/270243-need-help-with-only-inserting-numbers-in-textfield/#findComment-1389939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.