Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help with REGEX to block invalid characters

Enthusiast ,
Jul 21, 2011 Jul 21, 2011

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

TOPICS
Advanced techniques
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 22, 2011 Jul 22, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 22, 2011 Jul 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 04, 2011 Oct 04, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 04, 2011 Oct 04, 2011

Hi,

Please try this

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 04, 2011 Oct 04, 2011
LATEST

The answer I came up with myself actually did the trick

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources