feha Posted September 4, 2009 Share Posted September 4, 2009 :rtfm:/^[a-z0-9]+(?:[-][a-z0-9]+)*$/ I need help with this ... it should let the user name type : aa10-233-22 etc and it seams to work ok what i need is how to add to this regex the min max limit in total ex {2,50} so the user name will be min 2 and max 50 chers including hypens ... Sorry i'm not that good in regex ... The reg-ex should: validate: xx-xxx-xxxx-xx-xx (x: any length and alphanumeric lower case ...) should not validate: xx--xx---xx etc should not begin and end with - (hypen) and should have min-max limit ... for any help thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/ Share on other sites More sharing options...
.josh Posted September 4, 2009 Share Posted September 4, 2009 function validateString($string) { if (strlen($s) > 1 && strlen($s) < 51) { if (preg_match('~^[a-z0-9]+-?[a-z0-9]+$~i',$s)) return true; } return false; } Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912758 Share on other sites More sharing options...
feha Posted September 4, 2009 Author Share Posted September 4, 2009 Hi Cryon Thank you, this should work ok ... how about something like: /^[a-z0-9]+-?[a-z0-9]+$/ using {2,50} ? can this be done ? Thank You Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912770 Share on other sites More sharing options...
feha Posted September 4, 2009 Author Share Posted September 4, 2009 also the best working reg-ex for this i got: /^[a-z0-9]+([-]?[a-z0-9])*$/ this works ok as: aaa-100-aa-1000 etc ... Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912776 Share on other sites More sharing options...
.josh Posted September 4, 2009 Share Posted September 4, 2009 you can add range to the regex but it's more optimal to use the strlen condition first. strlen is a lot faster than using {2,50} in a pattern so it's better to pre-qualify the string like that, instead of putting it in the regex. If it doesn't validate on the strlen condition, you also save php the work of having to go through the rest of the regex. Regex is a powerful tool, but wherever possible, you should always use built-in functions. Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912782 Share on other sites More sharing options...
feha Posted September 4, 2009 Author Share Posted September 4, 2009 you can add range to the regex but it's more optimal to use the strlen condition first. strlen is a lot faster than using {2,50} in a pattern so it's better to pre-qualify the string like that, instead of putting it in the regex. If it doesn't validate on the strlen condition, you also save php the work of having to go through the rest of the regex. Regex is a powerful tool, but wherever possible, you should always use built-in functions. Thanks Cryon U'r right, but some times it is needed just in single line (as i need it now ) The problem is i can't get it work if i try {2,50} (i don't know where to place it on /^[a-z0-9]+([-]?[a-z0-9])*$/ , as i get errors) I'm not an expert in regex ... Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912792 Share on other sites More sharing options...
.josh Posted September 4, 2009 Share Posted September 4, 2009 give me a legit reason why you need a one line regex solution. Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912794 Share on other sites More sharing options...
feha Posted September 4, 2009 Author Share Posted September 4, 2009 give me a legit reason why you need a one line regex solution. I'm using an IDE Code Generator, so it has the validation field for reg-ex, i can't put there any function except the reg-ex it self. Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912797 Share on other sites More sharing options...
.josh Posted September 5, 2009 Share Posted September 5, 2009 fair enough. This should work: /^[a-zA-Z0-9](?:[a-zA-Z0-9]|(?!--)-){0,48}[a-zA-Z0-9]$/ Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912829 Share on other sites More sharing options...
.josh Posted September 5, 2009 Share Posted September 5, 2009 er..I guess your OP said all lowercase so remove all the A-Z in there Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912831 Share on other sites More sharing options...
feha Posted September 5, 2009 Author Share Posted September 5, 2009 fair enough. This should work: /^[a-zA-Z0-9](?:[a-zA-Z0-9]|(?!--)-){0,48}[a-zA-Z0-9]$/ Thank you very much, it works great :-) I guess if i wan min 3 chars i need to change {0,48} to {0,47} :-) Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912842 Share on other sites More sharing options...
.josh Posted September 5, 2009 Share Posted September 5, 2009 no actually you would need to do {1,48} The first character class and last character class account for the minimum of 2. {0,48} will match nothing up to 48 characters. so to make it a minimum of 3, you'd up the minimum range, not lower the max. Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912847 Share on other sites More sharing options...
feha Posted September 5, 2009 Author Share Posted September 5, 2009 no actually you would need to do {1,48} The first character class and last character class account for the minimum of 2. {0,48} will match nothing up to 48 characters. so to make it a minimum of 3, you'd up the minimum range, not lower the max. Great, this was very nice solution, thank you again for your great help. I'll need to learn more about RegEx :-) Quote Link to comment https://forums.phpfreaks.com/topic/173165-solved-need-help-with-user-name-validation/#findComment-912850 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.