Skip to main content
Inspiring
July 21, 2011
Question

Help with REGEX to block invalid characters

  • July 21, 2011
  • 2 replies
  • 1543 views

I have a regex that is used to block unusual characters from being entered into a user name, so they can put pipes etc in there, I just want 0-9 and a-z (upper or lowercase), but I just noticed that it's not working. I am not up to speed on regex, I took this from somewhere else

here is the expression:

<cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z][A-Za-z0-9_]*', '', 'ALL'))) gt 0>

It is failing when I enter 2kljlkll3456 as the username

Anybody have any idea why it's not working?

After posing this I found out that the issue is that it does not allo me to have a username that starts with a number, only a letter, anybody have any idea how to fix that?

Thanks

Mark

This topic has been closed for replies.

2 replies

Dileep_NR
Inspiring
October 4, 2011

Hi,

Please try this

<cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z|0-9][A-Za-z0-9_]*', '', 'ALL'))) gt 0>

ACS LLCAuthor
Inspiring
October 4, 2011

The answer I came up with myself actually did the trick

Inspiring
July 22, 2011

I didn't know the answer, so I googled "regex alphanumeric".  Now I do.

ACS LLCAuthor
Inspiring
July 22, 2011

Hey Dan,

I found a link that explained how the regex is actually formed which helped!

http://stackoverflow.com/questions/336210/regular-expression-for-alphanumeric-and-underscores

Now I have managed to get a basic understanding of how they are formed the fix was easy

I had:

<cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z][A-Za-z0-9_]*', '', 'ALL'))) gt 0>

But should have had
<cfif len(trim(ReReplaceNocase(form_username, '^[A-Za-z0-9_]*', '', 'ALL'))) gt 0>

Simply removing [A-Za-z] from the start fixed it. I get it now ... so the first section defined the first character which was restricted to A-Za-z only.

I'll mark this as answered

Thanks

Mark

Participant
October 4, 2011

I am sure that this post which gives us an answer regarding REGEX to block invalid characters will be useful for others who are facing the same issue!! The link here seems to have helped him to solve the problem so I am sure that it will be worth checking out!! It is so frustrating when we come up with such unexpected obstacles and then to finally be able to find an answer it without anyone’s help!!

Adrian |  Affordable small business seo