บทความอื่นๆ ในหมวดเดียวกัน



  Search Articles


    

Expression Validate for Good Password

Article By : 
 gobElmo
Reference : 
 http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C4F005D3717
Read/Comment : 
 554 / 19

When it comes to password validation using regular expressions, things can get a bit complicated. Normally, you want people to enter a "good" password that has a mix of numbers and letters. But you may not care where the numbers and letters appear. So you re not looking for a "pattern" in the string. You just want a letter somewhere and a number somewhere.

In this first example, the password must be at least 8 characters long and start and end with a letter.

var re = /^[A-Za-z]w{6,}[A-Za-z]$/;
if (!re.test(myString)) { alert("Please enter a valid password!"); }

The ^ looks for something at the start of the string. The brackets indicate the valid character set. So it must start with an upper or lower case letter. After that, the w means there can be valid alphanumeric characters (numbers 0-9, upper/lower case letters a-z, the underscore) and says there must be at least 6 (but no upper limit). Then comes another set and the $ looks for something at the end of the string. So this statement says there must be a letter, then at least 6 of any alphanumeric characters, then a letter (making 8 the minimum number of characters).

In this second example, the password length doesn t matter, but the password must contain at least 1 number, at least 1 lower case letter, and at least 1 upper case letter.

var re = /^w*(?=w*d)(?=w*[a-z])(?=w*[A-Z])w*$/
if (!re.test(myString)) { alert("Please enter a valid password!"); }

Again, the ^ and $ are looking for things at the start and end. The "w*" combination is used at both the start and the end. w means any alphanumeric character, and * means zero or more. You ll see why it s "zero or more" in a bit. Between are groupings in parentheses. The "(?" combination is a flag in regular expressions. Basically, they say "apply the following formula, but don t consume any of the string". In this example, instead of specifying the order that things should appear, it s saying that it must appear but we re not worried about the order.

The first grouping (called an "atom" in "regular expresion speak") uses the = sign. This means that there must be a match. Other choices are ! for a negative match (the string must not look like this). There are others (more complicated) for preceeding matches and stuff. We can refer you to a
regular expression syntax web site for further details.

After the = sign comes "w*d". Again, any alphanumeric character can happen zero or more times, then any digit (d means any digit from 0 to 9) can happen. So this checks to see if there is at least one number in the string. But since the string isn t comsumed, that one digit can appear anywhere in the string.

The next atom (grouping) is (?=w*[a-z]). This is similar to the digit grouping, except it looks for a lower case letter. Again, the lower case letter can appear anywhere, but there has to be at least one.

The third atom is (?=w*[A-Z]) which looks for an upper case letter somewhere in the string.

Finally, at the end is zero or more alphanumeric characters. To match this string, the minimum characters needed is 3 (one upper case letter, one lower case letter, and one number).

Comment :  1 29/9/2551 11:38:13
  By :  QWMbvIfupOHneT
Comment :  2 29/9/2551 12:50:05
  By :  IDtAxvWnTWbowefVave
Comment :  3 29/9/2551 17:30:46
Good morning. Two of my colleagues were at Goodwood the day of your accident and were shocked by what happened gold Nothing going on , but shrug. gold other,   By :  SlEBrOUjOYOBLsAFJkT
Comment :  4 29/9/2551 17:31:23
Good morning. Two of my colleagues were at Goodwood the day of your accident and were shocked by what happened gold Nothing going on , but shrug. gold other,   By :  SlEBrOUjOYOBLsAFJkT
Comment :  5 30/9/2551 13:22:47
Good morning to all of you... still, and that is a rare and beautiful thing gold Very cool! gold divide, gold reach,   By :  NxGOuQgYnYiOOEtW
Comment :  6 1/10/2551 19:40:26
Hi all!!! Up until 2001 I lived in Sidcup and began marshalling at Brands in 1998 gold More or less nothing seems worth bothering with. gold track,   By :  lDKzCjmDOiF
Comment :  7 2/10/2551 8:23:35
Good morning to all of you... well sort of gold Very doll! gold well, gold under,   By :  CpAoIkLMjs
Comment :  8 4/10/2551 22:41:18
  By :  qeqmnBFpv
Comment :  9 6/10/2551 2:07:35
  By :  htuINHRYVIOw
Comment :  10 9/10/2551 0:00:18
  By :  GstGWrieYkTqLbHXf
Comment :  11 10/10/2551 19:56:26
  By :  TFCpEjpopIHIq
Comment :  12 22/10/2551 20:24:58
  By :  uAnxjzAUpNFQXv
Comment :  13 22/10/2551 20:25:32
  By :  uAnxjzAUpNFQXv
Comment :  14 12/6/2552 17:18:02
  By :  uqTRJsBNpp
Comment :  15 19/10/2552 1:20:23
  By :  OZsNmPGySoMPc
Comment :  16 19/10/2552 1:49:36
  By :  RLBJEFrvfyZ
Comment :  17 27/10/2552 2:44:21
  By :  bqgGJfzPUZzkpZxaX
Comment :  18 24/12/2552 7:53:13
  By :  comment2, celexa online,
Comment :  19 20/6/2553 0:28:05
  By :  XbGOoGPlrxsWEB

Your comment
 
Your name