Help generating Regular Expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pranaysharmadelhi
    New Member
    • Dec 2008
    • 1

    Help generating Regular Expression

    I want to generate a regular expression for password check.(ASP.Net)
    But javascript is what it really is.
    I would like to enforce Minimum 6 alphanumeric characters with minimum 1 numeric(0-9) and 1 character(a-z) or 1 Special Character(. or _)
    So far I have been able to generate something so they enter 6 alpha numberic characters, but don't know a way to check presence of 1 numeric and 1 alpha-numeric characted(or special).

    "^([A-Za-z0-9\._]){6}$"

    Please help.
  • xNephilimx
    Recognized Expert New Member
    • Jun 2007
    • 213

    #2
    I think the best way to do that is by doing three checks, it may be able to do that in one single regexp pattern, but I think it's not worth the time and effort, you can do this:

    [CODE=javascript]
    //the following string will validate, while if it only has numbers or only letters, it won't
    var s = 'a.b123-ic83_87s';

    if( s.match(/^[\w\._-]{6,}$/i) ) { //any word character, dashm dot or underscore, minimum 6 chars
    if( s.match(/([a-z]+)/i) && s.match(/([0-9]+)/i) ) { //if it contains at least a number AND a letter
    alert('ok');
    }
    }

    [/CODE]

    Comment

    Working...