Jump to content

How to restrict inputs to a form


YigalB

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/225559-how-to-restrict-inputs-to-a-form/
Share on other sites

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?

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.

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.

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

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.

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.