vijdev Posted June 7, 2010 Share Posted June 7, 2010 $input=hjkhk$#@$@; $test=preg_match_all("/^[A-Za-z]{1,20}/",$input,$match); why does it pass $#@$@, although my definition ensures only alphas? how do i ensure $#@$@ is not passed, or anything else from the keyboard like :;{"}[]! is not passed? please help! Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/ Share on other sites More sharing options...
premiso Posted June 7, 2010 Share Posted June 7, 2010 If I am not mistaken the {1,20} means 1 OR 20 not one through 20. Chances are that is your issue. Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068776 Share on other sites More sharing options...
vijdev Posted June 7, 2010 Author Share Posted June 7, 2010 If I am not mistaken the {1,20} means 1 OR 20 not one through 20. Chances are that is your issue. I want to have minimum one character and maximum 20 characters for that input field. Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068781 Share on other sites More sharing options...
premiso Posted June 7, 2010 Share Posted June 7, 2010 Better to do that check outside of regex: $stringLen = strlen($string); if ($stringLen < 1 || $stringLen > 20 || !preg_match("/[A-Za-z]*/",$input) ) { echo 'You have provided invalid input. Valid input ......'; } Untested, pending any retarded errors I made should work for what you want. Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068783 Share on other sites More sharing options...
vijdev Posted June 7, 2010 Author Share Posted June 7, 2010 Better to do that check outside of regex: $stringLen = strlen($string); if ($stringLen < 1 || $stringLen > 20 || !preg_match("/[A-Za-z]*/",$input) ) { echo 'You have provided invalid input. Valid input ......'; } Untested, pending any retarded errors I made should work for what you want. thanks, but would this solve my initial problem of not passing $#@$@ ? Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068785 Share on other sites More sharing options...
Goldeneye Posted June 7, 2010 Share Posted June 7, 2010 What exactly do you want this regular expression to do? You want it to match alphabetic-characters at the beginning of a string? If so, it appears to be working fine for me: <?php $input='hjkhk$#@$@'; preg_match_all("/^[A-Za-z]{1,20}/",$input,$match); print_r($match); ?> produces: Array ( [0] => Array ( [0] => hjkhk ) ) Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068789 Share on other sites More sharing options...
newbtophp Posted June 7, 2010 Share Posted June 7, 2010 If I am not mistaken the {1,20} means 1 OR 20 not one through 20. Chances are that is your issue. Hmm I've always taught {1,20} meant the range of characters accepted, so in this case characters between 1-20 are allowed. But thanks for the correction. Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068791 Share on other sites More sharing options...
premiso Posted June 7, 2010 Share Posted June 7, 2010 thanks, but would this solve my initial problem of not passing $#@$@ ? Did you try it? The reason it was returning true was because 1 or 20 had an alpha character, which was a valid response. Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068800 Share on other sites More sharing options...
vijdev Posted June 7, 2010 Author Share Posted June 7, 2010 Ok, Let me put again the problem definition for clarity: 1.I need to check if input is only alphabets, not one single other type character 2.Needs to be minimum 1 char and max 20 characters long This is what am intending to do with above expression.Any directions?..have to admit it looks so simple! :| Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068825 Share on other sites More sharing options...
DavidAM Posted June 7, 2010 Share Posted June 7, 2010 Unless I am terribly mistaken, {1,20} means anywhere between 1 and 20 (inclusive) occurances of the preceeding class. In this case, your RegExp will match any string that starts with 1 or more alphabetic characters (upper or lower case) regardless of how long the string is. It will match this entire paragraph. I think if you anchor the RegExp to the end of the string as well, then it will do what you are expecting: $input='hjkhk$#@$@'; $test=preg_match_all("/^[A-Za-z]{1,20}$/",$input,$match); The caret ( ^ ) anchors to the start of the string and the dollar-sign ( $ ) anchors to the end. So now it should match a string of 1 to 20 alphabetic characters. I am no RegExp expert, not even a little bit. But I think this is correct. Give it a go Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068837 Share on other sites More sharing options...
ZachMEdwards Posted June 7, 2010 Share Posted June 7, 2010 Or using case-insensitive: $pattern = '/^[A-Z]{1,20}$/i'; Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068850 Share on other sites More sharing options...
vijdev Posted June 7, 2010 Author Share Posted June 7, 2010 thanks guys, the $ sign did help!...too many things to remember for a newbie like me! but it was a great learning one!thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/204051-why-does-this-matching-fail/#findComment-1068957 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.