Forum Moderators: coopster

Message Too Old, No Replies

check user input is numeric or character string data

         

yllai

3:47 am on Aug 27, 2004 (gmt 0)

10+ Year Member



i have a form (in php) for user to submit their information...for example: postcode, i need to makesure user entering numeric value for postcode but not character string after user clcik on 'Submit'..how can i do it? same as otherwise, i need to makesure user not entering numeric value for field that require character string value.

Any example?

ergophobe

4:48 am on Aug 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's say the field is called postcode, I do


if ( isset($_POST['postcode'])
&& is_numeric($_POST['postcode'])
&& is_int($_POST['postcode'] + 0)
)

basically, line by line
- is the variable set?
- post data from an <input> is a string, so we check whether the string is numeric
- if it is, we cast it as a number (which type depends on the data) by adding zero (or multiplying by 1) and check to see whether the number is an integer of a float.

Tom