YigalB Posted January 24, 2011 Share Posted January 24, 2011 What is the best way to force a user to input numeric value into a form, with the following condition: Either the number is an integer (positive or negative or zero), or non integer but limited to the one digit behind the dot (e.g. 1.2 is valid, but 1.21 is not)? I know I can test it in the server side, but I prefer it to be on the form side. Can it be in the HTML level? Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/ Share on other sites More sharing options...
codefossa Posted January 24, 2011 Share Posted January 24, 2011 Javascript to only allow numbers to be entered and check the submitted field value server-side before using it. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164681 Share on other sites More sharing options...
XistenceNL Posted January 24, 2011 Share Posted January 24, 2011 Remember Javascript is not bulletproof :-\ If somebody disables javascript you're still in trouble. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164684 Share on other sites More sharing options...
YigalB Posted January 24, 2011 Author Share Posted January 24, 2011 Does it mean there is no way to tell the HTML (without using Javascript) to accept only numeric values? Assuming no such way, I will prefer doing it on the server side. In that case, are there built-in functions that will help me to identify if the value of the field is 100% numeric, or integer etc? Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164690 Share on other sites More sharing options...
codefossa Posted January 24, 2011 Share Posted January 24, 2011 Remember Javascript is not bulletproof :-\ If somebody disables javascript you're still in trouble. That's the reason for using PHP to double-check it server-side and lettin' them know that they cannot enter alpha characters there. The Javascript would stop most people though without them having to go back and fix it after posting. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164693 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 Does it mean there is no way to tell the HTML (without using Javascript) to accept only numeric values? Assuming no such way, I will prefer doing it on the server side. In that case, are there built-in functions that will help me to identify if the value of the field is 100% numeric, or integer etc? You must validate form input server-side. Since all form data is, by default, a string value, you'd need to use a function such as ctype_digit, then cast the value as a numeric type if it validates. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164862 Share on other sites More sharing options...
YigalB Posted January 25, 2011 Author Share Posted January 25, 2011 If all form data is string, I understand I must validate at server side. I am not sure though what's the difference between ctype_digit() and is_numeric() and what is better/recommended. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164877 Share on other sites More sharing options...
gristoi Posted January 25, 2011 Share Posted January 25, 2011 Hi, it might be a bit of overkill for what you need but you could try looking at using Zend_Form. This has the ability to let you set validators for each field, such as integers, alphanumeric etc..., without using JavaScript. But you will need a basic understanding of using objects. Have a look here http://framework.zend.com/manual/en/zend.form.html for more info. Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1164891 Share on other sites More sharing options...
Pikachu2000 Posted January 25, 2011 Share Posted January 25, 2011 If all form data is string, I understand I must validate at server side. I am not sure though what's the difference between ctype_digit() and is_numeric() and what is better/recommended. In a nutshell, ctype_digit will look at a string and return true if every character in it is a number. is_numeric returns true if the data is of a numeric type or is a numeric string. A numeric string includes decimals, and exponential notation, where ctype_digit does not. The manual actually explains the functions quite well, and it would behoove you to read those entries, and browse through the related functions in the sidebar. Quote Link to comment https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/#findComment-1165082 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.