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
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?

Link to comment
Share on other sites

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.
Link to comment
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?

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.