cc4digital Posted November 30, 2010 Share Posted November 30, 2010 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. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 30, 2010 Share Posted November 30, 2010 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2010 Share Posted November 30, 2010 You don't need to use a pattern for that. You can use ctype_digit to check that string type data contains only numbers, or is_int to check numeric data types. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted November 30, 2010 Share Posted November 30, 2010 ctype_digit is more accurate, Pikachu is right. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 1, 2010 Share Posted December 1, 2010 If you insist on keeping with the regex route, pattern would be: $pricecheckpattern ="#^[0-9]+$#"; (you also forgot to specify end of line anchor) Quote Link to comment Share on other sites More sharing options...
cc4digital Posted December 1, 2010 Author Share Posted December 1, 2010 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. 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted December 1, 2010 Share Posted December 1, 2010 look at sticky in this forum Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.