Skip to main content
Inspiring
February 2, 2011
Answered

Regex not working

  • February 2, 2011
  • 3 replies
  • 906 views

Can anyone tell me why my regex is not working on my cfinput textbox please. I only want to allow a-z as available charactors to enter and the below code fails.

<cfinput type="text"
        id="surname"
        name="surname"
        class="txt"
        title="Surname"
        value="#presentsurname#"
        validate="regex"
        pattern="[a-z]"
        message="Please enter a valid Surname."
        maxlength="60" />

If i type in $$$ then the regex is flagged up and I get the error message displayed.

Although if I type in a$$, I get no error message.


Thanks,

G

    This topic has been closed for replies.
    Correct answer Owainnorth

    Try putting the start and end chars in, ie ^ at the start and $ at the end to make sure it's matching the entire string.

    3 replies

    February 2, 2011

    Looks like you found a fix,

    There is a tool I use called RegexBuddy that helps me debug Regular Expressions in real time. Also has a gui to help you build a Regular expressions, thats how I started but now I can write them with no problems.

    http://www.regexbuddy.com/

    Participating Frequently
    February 2, 2011

    The regex you are using is only checking the first character in the string - and only checking to make sure its lowercase.

    Try using *

    Owainnorth
    Inspiring
    February 2, 2011

    That would only allow one character, try [a-z]+ to allow one or more.

    _G_1Author
    Inspiring
    February 2, 2011

    Thanks for the quick replies guys, your really helpful and both regex examples you have given both work.

    To cut a long story short I was give the following regex to use against my surname textbox, and I need to follow 'their' standards so I must use this pattern.

    ([A-Z'\-]*)|([A-Z'\-][A-Z '\-]*[A-Z'\-])

    And this should only allow A-Z (in capitals) and any of the other special charactors that are mentioned in the regex....  Although this pattern is allowing me to enter A$$ and I do not understand why it is allowing the $ sign to be an enterable charactor when the dollar sign is not listed in the regex.

    Owainnorth
    OwainnorthCorrect answer
    Inspiring
    February 2, 2011

    Try putting the start and end chars in, ie ^ at the start and $ at the end to make sure it's matching the entire string.