password validation with reg exp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Obi-Too

    password validation with reg exp

    I need to validate a password to only allow A-Z, a-z and 0-9. I
    thought this would work:

    function Validate()
    {
    var passphrase = document.all.ch allenge.value;

    var re = /[a-zA-Z0-9]/;
    if (!re.test(passp hrase))
    {
    alert("Please enter a valid password!");
    return false;
    }
    else
    {
    return true;
    }
    }

    however if the following is entered 045# - this is accepted a being
    valid. What am i doing wrong?

    thanks,

    Obitoo
  • Lasse Reichstein Nielsen

    #2
    Re: password validation with reg exp

    obitoo@ziplip.c om (Obi-Too) writes:
    [color=blue]
    > I need to validate a password to only allow A-Z, a-z and 0-9. I
    > thought this would work:[/color]
    ....[color=blue]
    > var re = /[a-zA-Z0-9]/;[/color]

    This RE matches any string that contain at least one digit or letter,
    not just strings that contain only those.

    Try
    /^[a-z0-9]+$/i
    This matches from the beginning of the string (^) to the end ($) and
    between there are one or more letters or digits. The "i" means "ignore
    case", so we don't have to write both a-z and A-Z.

    Another test is
    /[^a-z0-9]/i
    this succeedes if the string contains any non-digit/letter. You can the
    use
    if (re.test(...)) { // password failed

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...