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
https://forums.phpfreaks.com/topic/220273-question-on-preg_match-function/
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

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

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.