Skip to main content
May 16, 2008
Question

simple regex validation

  • May 16, 2008
  • 4 replies
  • 583 views
I need to match any number up to a 3 digits (e.g., 0 -> 999)

regex for that I think should be: \d{1,3}

my cfinput has: validate="regular_expression" pattern="\d{1,3}"

but it doesn't seem to be working - I can enter 1000 and it submits ok.

It seems to be there in the source ok:

//form element quan_20 'REGULAR_EXPRESSION' validation checks
if (!_CF_checkregex(_CF_this['quan_20'].value, /\d{1,3}/, true))

This is CF7 - am I missing something?
    This topic has been closed for replies.

    4 replies

    Inspiring
    May 20, 2008
    Adam Cameron wrote:
    >> The '&' specifies that this must be at the end of the string.
    >
    > "$" - not "&" - I reckon.
    >

    Yeah what Adam says. I was a zombie last Friday. My fingers where
    pretty much on their own and typed what they wanted.
    Inspiring
    May 16, 2008
    > The '&' specifies that this must be at the end of the string.

    "$" - not "&" - I reckon.

    --
    Adam
    Inspiring
    May 16, 2008
    I haven't used regular expressions in the validate attribute, but its possible that CF might not support "\d"? Have you tried:

    [0-9]{1,3}

    to see if that works?
    Inspiring
    May 16, 2008
    SteveLogan1029 wrote:
    > I need to match any number up to a 3 digits (e.g., 0 -> 999)
    >
    > regex for that I think should be: \d{1,3}
    > ...
    > This is CF7 - am I missing something?

    Not quite. your regex will match any string that has one to three
    digits in a row. It does not say anything about it the string contains
    anything else as well. I.E. '123', '1234' and 'a string with 69 a
    number in it' will all return true. Since they all have one to three
    digits somewhere in them.

    To do what you want you need to anchor the regex so that it does not
    allow anything else to exist.

    Regex: ^\d{1,3}&

    The '^' specifies that this must be at the beginning of the string.

    The '&' specifies that this must be at the end of the string.

    In other words a string with one to three digits and nothing else.