foochuck Posted September 15, 2008 Share Posted September 15, 2008 I have a basic text input field on a web form. I want to have my users type in 16 numbers ( it can be any sixteen numbers - as long as the count is 16 numbers exactly - I'm not checking it with a database ) Using a post method, the page submits the form to itself. Basically, I'm just wondering how I can tell if 16 numbers have been entered into my field? The field is named fm_id. I'm taking it from the $_POST and creating a variable called $id How can I count the number of numbers in $id ? Thanks FOO Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 15, 2008 Share Posted September 15, 2008 You can use explode() to convert this string into an array, and then check how many values are there in the resulting array. Quote Link to comment Share on other sites More sharing options...
Maq Posted September 15, 2008 Share Posted September 15, 2008 You could use a regular expression preg_all_match(); to find all single digit numbers. I think there's a parameter that keeps track of the count. Although, there may be an easier way with javascript... Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 15, 2008 Share Posted September 15, 2008 if (ctype_digit($input) && strlen($input)==16) { echo "The input is numeric and is 16 characters long"; } else { echo "The input is not numeric and/or is not 16 characters long"; } Quote Link to comment Share on other sites More sharing options...
foochuck Posted September 15, 2008 Author Share Posted September 15, 2008 Thanks mjdamato - that did the trick. if (ctype_digit($input) && strlen($input)==16) { echo "The input is numeric and is 16 characters long"; } else { echo "The input is not numeric and/or is not 16 characters long"; } Quote Link to comment Share on other sites More sharing options...
jmurch Posted September 15, 2008 Share Posted September 15, 2008 Foo, Have you tried JS. You can give the users immediate feedback instead of waiting to post to the server for the check. Jeff Quote Link to comment Share on other sites More sharing options...
Maq Posted September 15, 2008 Share Posted September 15, 2008 Thanks mjdamato - that did the trick. This problem has been solved already. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 15, 2008 Share Posted September 15, 2008 Oh... you meant 16 digits not 16 numbers... Quote Link to comment Share on other sites More sharing options...
Maq Posted September 15, 2008 Share Posted September 15, 2008 No, I think he meant numbers. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 15, 2008 Share Posted September 15, 2008 mjdamato's solution works for digits Quote Link to comment Share on other sites More sharing options...
Maq Posted September 15, 2008 Share Posted September 15, 2008 Yeah sorry :-X I looked at his solution and thought he checked for numerics... Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 15, 2008 Share Posted September 15, 2008 Have you tried JS. You can give the users immediate feedback instead of waiting to post to the server for the check. Javascript IS a great way to give the user immediate feedback, however it should NOT be relied upon as the only validation. You must ALWAYS have server-side validation. User can have JS turned off or submit data without using your form. Quote Link to comment Share on other sites More sharing options...
foochuck Posted September 15, 2008 Author Share Posted September 15, 2008 Good point mj. Thanks again. Have you tried JS. You can give the users immediate feedback instead of waiting to post to the server for the check. Javascript IS a great way to give the user immediate feedback, however it should NOT be relied upon as the only validation. You must ALWAYS have server-side validation. User can have JS turned off or submit data without using your form. Quote Link to comment 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.