gergy008 Posted June 20, 2010 Share Posted June 20, 2010 Hi I'm still not that good with regex as I don't do it very often but can someone help my out please? I need regex that will only allow alpha numberic characters and deny anything else, ^[a-zA-Z0-9]+$ doesn't seem to work. Thanks for your help!! Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/ Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 ~^[a-z0-9]+$~i Would work. But you don't need regex for this. Just use ctype_alnum. Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074721 Share on other sites More sharing options...
gergy008 Posted June 20, 2010 Author Share Posted June 20, 2010 ~^[a-z0-9]+$~i Would work. But you don't need regex for this. Just use ctype_alnum. It's not exactly just for php, I'm going to be using it in javascript too Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074724 Share on other sites More sharing options...
salathe Posted June 20, 2010 Share Posted June 20, 2010 doesn't seem to work. What makes you think that? Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074730 Share on other sites More sharing options...
gergy008 Posted June 20, 2010 Author Share Posted June 20, 2010 doesn't seem to work. What makes you think that? I use it in php too... The php version does the same thing as the javascript one, It recognises anything with a letter or a number in it, And doesn't reject if it has a symbol in it. Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074731 Share on other sites More sharing options...
Alex Posted June 20, 2010 Share Posted June 20, 2010 Sure it does, try this code: if("test%".match('^[a-zA-Z0-9]+$')) { alert('matched'); } else { alert('did not match'); } Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074733 Share on other sites More sharing options...
salathe Posted June 21, 2010 Share Posted June 21, 2010 I use it in php too... The php version does the same thing as the javascript one, It recognises anything with a letter or a number in it, And doesn't reject if it has a symbol in it. Show us a short sample code which reproduces the issue of not rejecting subject strings containing symbols. The only non-alphanumeric symbol that should be allowed by that regex (in PHP/PCRE) is a trailing newline character. Quote Link to comment https://forums.phpfreaks.com/topic/205352-simple-regex-help/#findComment-1074894 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.