Jump to content

Couple field validation questions


Mazer14

Recommended Posts

Having trouble figuring this out.

 

1) How can I check whether the user's input for a field is an integer with a value greater than 0?

 

Was thinking of using regular expressions using 1-9 but then 10 might result in an error and that's not wanted.

 

2) For a field that's a drop-down, how can I have an error show if they choose the default option (value of "none")

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/230164-couple-field-validation-questions/
Share on other sites

Cool, so I got those worked out.

 

One more question though. Trying to have an error occur when they leave a text field blank:

 

if (empty($_POST['name']) || $_POST['name'] == " " || $_POST['name'] == "  " || $_POST['name'] == "  ")

 

Is there a method to make an error occur any time a string of spaces are entered instead of having to do " " and "  " and "  " and so on?

Should work but post back with any problems...

 

1)

if(preg_match('/([0-9]{1,})/', $thing_to_check))
{
     // GOOD
}
else
{
     // ERROR
}

 

 

As is, the above regular expression will match anything that contains a number; including "a2". It should work however by changing it to:

 

<?php
...

if(preg_match('/^([0-9]{1,}$)/', $thing_to_check) && $thing_to_check > 0) {
     // GOOD

} else {
     // ERROR
}

...
?>

 

 

Note that I also added the secondary check to make sure it doesn't match 0.

Cool, so I got those worked out.

 

One more question though. Trying to have an error occur when they leave a text field blank:

 

if (empty($_POST['name']) || $_POST['name'] == " " || $_POST['name'] == "  " || $_POST['name'] == "  ")

 

Is there a method to make an error occur any time a string of spaces are entered instead of having to do " " and "  " and "  " and so on?

 

You could trim the value before testing it:

 

<?php
...

$_POST['name'] = trim($_POST['name']);

...
?>

 

 

For more information about trim(), check out:

http://php.net/manual/en/function.trim.php

There's no need to use a pattern for number 1.

 

if( is_numeric($_POST['value']) && intval($_POST['value']) > 0 ) {
// value validates
}

 

 

 

As long as you're ok with things like the following being considered a number:

+01.45

2.45

 

 

I didn't mean is_numeric(); I never even use that myself. I meant ctype_digit() . . .

 

if( ctype_digit($_POST['value']) && intval($_POST['value']) > 0 ) {
// value validates
}

I didn't mean is_numeric(); I never even use that myself. I meant ctype_digit() . . .

 

if( ctype_digit($_POST['value']) && intval($_POST['value']) > 0 ) {
// value validates
}

 

Nice, I've been wondering if there was an alternative to regular expressions. A lot of people suggest is_numeric().

 

ctype_digit() appears to be that answer.

http://php.net/manual/en/function.ctype-digit.php

 

 

Out of curiosity, why did you include intval()? The code seems to work fine without it. It doesn't throw any warnings either with

error_reporting(-1);

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.