AV1611 Posted May 11, 2007 Share Posted May 11, 2007 Easy one: Sorry, but I can never get regex right... I need to verify an input is 4 digits ONLY (not MORE or LESS) numbers ONLY must be greater than 0100 Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/ Share on other sites More sharing options...
StormTheGates Posted May 11, 2007 Share Posted May 11, 2007 Uhm how about this: $length = strlen($input); if($length != 4 OR $input < 100) { echo "Must be 4 chars and over 100!"; } Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250562 Share on other sites More sharing options...
AV1611 Posted May 11, 2007 Author Share Posted May 11, 2007 What happens if they input "ABCD"? Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250594 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 You have two choices, www.php.net/REGEX or modifying the above example as so. <?php $length = strlen($input); if(($length != 4 OR $input < 100) OR !is_numeric($input)) { echo "Must be 4 chars and over 100!"; } ?> www.php.net/ereg That is probably the better way to go is regex, but the above example should work too. Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250599 Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 would the addition of is_numeric help? edit: beaten to it Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250603 Share on other sites More sharing options...
AV1611 Posted May 11, 2007 Author Share Posted May 11, 2007 There are so many functions... How do you guys remember all them? This solves my problem, but I really need to find someone to help me with regex... after 2 years and 2 major web apps I still can't do them Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250612 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 I don't. Anytime I want something that I do not know if it exists or not I goto www.php.net and do a search. For this one I searched for numeric. Heck give it a try: www.php.net Search functions for numeric or number and see what comes up! That is what I love about PHP the whole manual is online and easily searchable with real world examples and user contributions. There is no function that you should not be able to understand in PHP or be able to not find. Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250615 Share on other sites More sharing options...
StormTheGates Posted May 11, 2007 Share Posted May 11, 2007 if(ereg("[^[:digit:]]", $input)) { echo "Not a good amount!"; } Makes sure its only numeric. However I think that if(!ctype_digit($input)) { $error = "Not a good amount!"; } Is better Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250616 Share on other sites More sharing options...
AV1611 Posted May 11, 2007 Author Share Posted May 11, 2007 would if(ereg("[^1-9]", $input)) be valid/correct? Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250622 Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 possibly: $result = preg_match('/([0-9]{4})/', $input, $matches); however it wouldnt determine if the number is at least 100... edit added boundaries, as would have otherwise find ANY group of 4 numbers: $result = preg_match('/^([0-9]{4})$/', $input, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250624 Share on other sites More sharing options...
StormTheGates Posted May 11, 2007 Share Posted May 11, 2007 Do 0-9 instead. Otherwise 0 will be excluded Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250625 Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 had a quick play with it. not properly tested and very messy, but seems to work..? <?php $result = preg_match('/^(([0-9][1-9]|[1-9][0-9])[0-9]{2})$/', $input, $matches); ?> which (i think) checks the first 2 digits to make sure that at least one of them is greater than 0 (1000 or 0100, for example) seems to do everything you want. Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250628 Share on other sites More sharing options...
AV1611 Posted May 11, 2007 Author Share Posted May 11, 2007 ... '/^ ... $/' ... I snipped away all the part I understand. What you have left is the syntax part, which is what I don't understand ' ' <--- single quotes, as that's the string / / <--- why do I need those? ^ <--- what's that for? $ <--- does that have something to do with the end of the string? I've read it in the manual a dozen times, but I just can't "get it" Thanks guys... Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250657 Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 /'s enclose the regex statement. the caret (^) signifies that we're looking from the start, the $ signifies the end of the string. so in English, the regex says: from the start of the string to the end, find 2 numbers that can't be both zero, as well as 2 further numbers. in all, find 4 numbers. (i think lol) there's a board specifically for regex here - some of the posts/stickies may be of assistance. in my case, regex is very much about trial and error as opposed to actually knowing Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250661 Share on other sites More sharing options...
AV1611 Posted May 11, 2007 Author Share Posted May 11, 2007 As always, You remain my hero Red ;D Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250664 Share on other sites More sharing options...
redbullmarky Posted May 11, 2007 Share Posted May 11, 2007 moved here from PHP Help as some of the people round here might have a different take on things Quote Link to comment https://forums.phpfreaks.com/topic/50937-solved-verify-input/#findComment-250666 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.