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

Link to comment
Share on other sites

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.

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

 

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

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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);

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.