Warptweet Posted December 18, 2007 Share Posted December 18, 2007 I have some forms on my site, and some of them require that you can only type in numbers, that means no decimals either. How can I check a string to see if it contains letters, decimals, or any of those complicated signs. AKA: Check if theres anything other than numbers 1-9 Thank you so much for any help! Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 if(preg_match('/^[0-9]+$')) { //1 or more numbers } Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 if(preg_match('/^[0-9]+$')) { //1 or more numbers } nope easier way ctype_digit(texthere) Quote Link to comment Share on other sites More sharing options...
Warptweet Posted December 18, 2007 Author Share Posted December 18, 2007 Thanks both of you. I used tengs method because I actually understood his, but thanks corbin too for trying. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Ahhh, mine was just a regular expression.... I didn't think to use teng's method.... Extending on teng's, you could also do: $input = $_POST['input']; $iinput = (int) $input; if($input != $iinput) { //not numeric } That might cause PHP to throw a notice or warning though.... Not sure. Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 Ahhh, mine was just a regular expression.... I didn't think to use teng's method.... Extending on teng's, you could also do: $input = $_POST['input']; $iinput = (int) $input; if($input != $iinput) { //not numeric } That might cause PHP to throw a notice or warning though.... Not sure. wont work because int will remove ZERO in front of your string right? eg.. this will give (int)' 0005' >>>5 Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Ohhh good call.... Didn't think of that.... I guess you could ltrim($input, '0') and then compare, but that would be much slower..... Hmmm.... I hadn't ever heard of ctype_digit before hehe.... Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 yah.. ctype data is cool Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 It's also extremely fast.... It took half the time as converting the input to int and left trimming 0s on my comp. It even beat: $iput = (int) $input; if($iput == 0 && $input != 0) { } By .09 seconds with 100,000 runs.... Quote Link to comment Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 Or is_numeric Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 Or is_numeric nope it will accept decimals is_numeric(0.343); // true Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 You could use is_numeric and strpos($input, '.'). Oddly enough, is_numeric combined with strpos is like .0001 seconds faster when the input isn't numeric, but when it is numeric, thus it has to continue to the strpos check, it's like 2.2 times slower lol.... 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.