Skip to main content
November 10, 2008
Question

Is Valid function

  • November 10, 2008
  • 5 replies
  • 562 views
i want to be able to validate that both a username and password submitted via a form contains uppercase and lowercase letters and the numbers 0-9, in any order. just not sure what to enter for the pattern:
<CFIF IsValid("regex", Form.UserName, pattern) AND IsValid("regex", Form.Password, pattern)>
    This topic has been closed for replies.

    5 replies

    Inspiring
    November 11, 2008
    Hi,

    Here's what I usually use:

    Defining the variables in the config script:
    ========================================
    <cfset application.password_regexp= "^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]).*$">
    <cfset application.password_info= "A strong password is required (include [A-Z], [a-z], [0-9] {6})">

    The verification:
    ========================================
    <cfif REFind(application.password_regexp,value) IS 0>
    <cfset PAGE_MESSAGE= PAGE_MESSAGE & "- #application.password_info#\n">
    </cfif>

    cheers,
    fober

    Inspiring
    November 10, 2008
    > tried that - it still accepts an all lowercase string with no numbers

    Right, so to clarify your original post, you want the string to have at
    least one each of these three things:
    - an uppercase letter
    - a lowercase letter
    - a digit.

    So:
    "Aa1" is fine, because it has all three.
    "AA1" is not fine because it doesn't haev any lowercase letters.
    "Aa1!" is not fine because it has a character other than letters and digit.

    Not just "some mix of characters including any of uppercase, lowercase and
    digits", which is what your post reads like.

    I didn't know the answer to this off the top of my head, but googled
    'password regex "upper case" and "lower case" and digits', and found the
    answer pretty much straight away.

    Google rocks.

    --
    Adam
    November 10, 2008
    thanks (not really with it today as i'm suffering from a very bad cold!), the following seems to do the trick:

    <CFIF IsValid("regex", Form.UserName, "^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$") AND IsValid("regex", Form.Password, "^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$")>
    Inspiring
    November 10, 2008
    Hi,

    Can please explain a bit more?
    November 10, 2008
    tried that - it still accepts an all lowercase string with no numbers
    Inspiring
    November 10, 2008
    > i want to be able to validate that both a username and password submitted via a
    > form contains uppercase and lowercase letters and the numbers 0-9, in any
    > order. just not sure what to enter for the pattern:
    > <CFIF IsValid("regex", Form.UserName, pattern) AND IsValid("regex",
    > Form.Password, pattern)>

    Have you read the docs on regexes, and have you had at least a stab at
    doing it yourself? What did you come up with?

    http://livedocs.adobe.com/coldfusion/8/regexp_03.html

    --
    Adam
    November 10, 2008
    yeah, did read the documentation and had tried this but didnt work.

    <CFIF IsValid("regex", Form.UserName, "[A-Za-z0-9]") AND IsValid("regex", Form.Password, "[A-Za-z0-9]")>
    Inspiring
    November 10, 2008
    Hi,

    See if this works,

    <cfif IsValid("regex",Form.txtusername,"^([a-zA-Z0-9])*") and IsValid("regex",Form.txtpassword,"^([a-zA-Z0-9])*")>

    HTH