Jump to content

Question on preg_match function


cc4digital

Recommended Posts

Hello Everyone;

 

What I want to do is validate the user has only put a whole number.  eg. 1  , 10 ,  or 100

 

What I do not want is them to put 10.1.

 

So what I have is--

$pricecheckpattern ="#^[0-9]{0}#";
if (preg_match($pricecheckpattern, $years))
{
	echo "This has passed validation for year {$years} <br />";
}
else
{
	echo "This has passed validation for year {$years}<br />";
}

 

The problem is it will accept 10.1, which I do not want. :'(

 

Any help on this would be GREAT. :wtf:

Link to comment
Share on other sites

The number in the curly braces is the number of times you want the item to match.  You're looking for ZERO matches.  Everything has zero occurrences of a digit.

 

That being said, don't use preg_match for this at all:

 

if ( intval($years) == $years ) {
  //whole number
}

-Dan

Link to comment
Share on other sites

Thanks everyone for your input.  Great.  answered my question a few different ways. ;)

 

Thanks, Crayon Violent for showing me the regex route.  I am finding regex a very difficult concept to master.  8)

 

Does anyone have any good recommendations on how to learn regex.  I like to use pregmatch(), but I just don't get the patterns. :'(

 

thanks chuck

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.