Jump to content

[SOLVED] Need help with user name validation


feha

Recommended Posts

: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

 

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 :-)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.