menwn Posted May 6, 2009 Share Posted May 6, 2009 Hello all, My question is this. How can one with a regular expression to guarantee that at least on digits exists in a string. Valid Example: ABdafd123 Invalid Example: ABCfgssf Thank you Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/ Share on other sites More sharing options...
RichardRotterdam Posted May 6, 2009 Share Posted May 6, 2009 Not great with regex but i think this will work preg_match('/[0-9]/', $text) edit do note that it will only check if it has a digit in it. It will not check if the remaining chars are letters Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/#findComment-827357 Share on other sites More sharing options...
menwn Posted May 6, 2009 Author Share Posted May 6, 2009 Thank you for your reply. I want to disable the ability to pass not alphanumeric characters. And this does not help a lot. Up to now I had: preg_match("/[^a-zA-Zα-ωΑ-Ω0-9]/", $text) It works ok but I can pass all letter strings which I want to forbid unless it has at least a digit in it. thanks Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/#findComment-827366 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 if (!preg_match('/[\d]+/', $text)) { print 'No digits!'; } Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/#findComment-828326 Share on other sites More sharing options...
Ken2k7 Posted May 7, 2009 Share Posted May 7, 2009 MrAdam, you don't need the + sign or the brackets. If it matches any single number, there's a digit so you don't need to check for more. Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/#findComment-828415 Share on other sites More sharing options...
Adam Posted May 7, 2009 Share Posted May 7, 2009 Ahh of course! Quote Link to comment https://forums.phpfreaks.com/topic/157060-php-regular-expressions/#findComment-828418 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.