validating form entries are integers

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

sametch
New php-forum User
New php-forum User
Posts: 17
Joined: Wed Jul 03, 2002 3:10 am

validating form entries are integers

Postby sametch » Wed Feb 26, 2003 6:04 am

I am designing a form to enter values into a MySQL database.

One field in the database needs to hold integers. I would like to be able to validate that the value a user enters into the associated form text box is an integer and not a double or text.

The problem I have is all numeric entries end up being type string.

Is there an easy way to validate that a user has typed in an integer?

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Wed Feb 26, 2003 7:40 am

one of the ways...
eregi("[0-9]", $yourwar)

User avatar
WiZARD
Moderator
Moderator
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol
Contact:

Re: validating form entries are integers

Postby WiZARD » Wed Feb 26, 2003 7:48 am

1 variant:

Code: Select all

if (is_int($input_by_user))

2 variant:

Code: Select all

$var=intval($input_by_user);

*JaH*
New php-forum User
New php-forum User
Posts: 80
Joined: Fri Jan 31, 2003 4:18 pm

Postby *JaH* » Sun Mar 02, 2003 1:13 am

Pejone wrote:one of the ways...
eregi("[0-9]", $yourwar)



that one is wrong :) or at least $yourwar = "123a" will also be true....then you should make it into:
ereg("^[0-9]$",$yourwar)

always a bitch those reg.exps ;)

User avatar
Oleg Butuzov
Last Samuray
Last Samuray
Posts: 831
Joined: Sun Jun 02, 2002 3:09 am

Postby Oleg Butuzov » Sun Mar 02, 2003 1:28 am

thanks =%

Godiwa
New php-forum User
New php-forum User
Posts: 40
Joined: Wed Mar 05, 2003 5:55 am

Postby Godiwa » Wed Mar 05, 2003 10:09 am

I have looked a little at this one since I am also writing a lil page where I need to check that the input is integer

Code: Select all

if (is_int($input_by_user))


was the one listed above and I wondered how I should use it since I only want it to do something if it is NOT integer

Could I be doing something like this:

Code: Select all

if (is_int($input_by_user)) {
   ;
   }
else {
 print "bonk on head you made an error";
}


or is there someone I can make it to check is_NOTint blabla and then the command and forget the else thing?

Jesper

*JaH*
New php-forum User
New php-forum User
Posts: 80
Joined: Fri Jan 31, 2003 4:18 pm

Postby *JaH* » Wed Mar 05, 2003 2:12 pm

if (! is_int($input_by_user))


the ! means 'is not' or turns the boolean around (depends on when you use it)


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron