PDA

View Full Version : Need some Regex help


3lou 55
08-24-2014, 08:45 AM
Hello there,

So we were getting annoyed on our forums with stupid registration names.
So we wanna setup a regex now so that they can't do this anymore.
I know that this feature is available in the admin panel and we will also use that.
I just need some help with the regex.

Basicly what we want is we wanna allow these chars a-z A-Z 1234567890 and one space inbetween the name.
The space inbetween the name is not a MUST, but if there are spaces in the name it should only be inside the name, and it should only be one.

If anyone could help me with this one it would be greatly appreciated.

kh99
08-24-2014, 09:15 AM
I think this works:
^[A-Za-z0-9]+\s?[A-Za-z0-9]+$

Here's a page that will allow you to test it: http://www.regexplanet.com/advanced/java/index.html (there are others out there if you google regex test).

3lou 55
08-24-2014, 10:45 AM
I think this works:
^[A-Za-z0-9]+\s?[A-Za-z0-9]+$

Here's a page that will allow you to test it: http://www.regexplanet.com/advanced/java/index.html (there are others out there if you google regex test).

Yep cheers, that worked!
Thanks alot.

Black Snow
08-27-2014, 12:44 PM
Yep cheers, that worked!
Thanks alot.

I found that some people use seperators more than once in a row so I had to make sure that the first and last character are not separators, and that there's never more than one separator in a row. Here's my way:

/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/

After matching one or more alphanumeric characters, if there's a separator it must be followed by one or more alphanumerics.

3lou 55
09-06-2014, 10:22 AM
I found that some people use seperators more than once in a row so I had to make sure that the first and last character are not separators, and that there's never more than one separator in a row. Here's my way:

/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/

After matching one or more alphanumeric characters, if there's a separator it must be followed by one or more alphanumerics.

I was just about to post to ask this question, cheers.
Thanks for the great help (y)