Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression Pattern Match 1

Status
Not open for further replies.

DB2Problem

Programmer
Oct 17, 2002
53
US
Hello ALL,

I have to come up with a pattern to match with fulfilling my following criteria for a password field

1. It must have to be at least 8 characters long
2. It must have atleast one number (digit)
3. It must have one capital letter (character)

please advise how should i form a pattern and use with match()

Thanks

 
Do you [!]HAVE[/!] to check for length with the other criteria all in the same regexp? You can do this, but you're looking at a VERY long regexp because you will have to check for all examples up to 8 characters. For example:
[small]
0 characters, 1 upper case character, 1 number, at least 6 more characters
or
0 characters, 1 number, 1 upper case character, at least 6 more characters
or
1 character, 1 upper case character, 1 number, at least 5 more characters
or
1 character, 1 number, 1 upper case character, at least 5 more characters
or
0 characters, 1 upper case character, 1 character, 1 number, at least 5 more characters
etc.[/small]

it would be [!]MUCH[/!] easier to make it a 2 step validation process (not to mention you would be able to customize your error messages to the specific problem):

Code:
<script type="text/javascript">

function validateText(str) {
   if (str.length < 8) {
      alert("String must be at least 8 characters");
      return false;
   }
   var a = str.match(/(.*[A-Z].*\d.*|.*\d.*[A-Z].*)/);
   if (a == null) {
      alert("String must contain at least 1 numeric character, and one upper case letter");
      return false;
   }
   return a;
   //a will contain an array of information regarding the matches
}

</script>

<input type="text" id="blah" />
<input type="button" value="validate" onclick="validateText(document.getElementById('blah').value)" />

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
I CAN NOT BELIEVE IT - THIS IS THE BEST ANSWER I EVER EVER GOT FROM THIS FORUM - THANKS A TONS

AMAZING AND EXTREEMLY HAPPY THAT YOU HAVE HELPED ME - I UNDERSTAND THE PATTERN AS WELL AND THANK YOU SOO MUCH
 
Glad to have helped, and thanks for the star.

-kaht

[small]How spicy would you like your chang sauce? Oh man... I have no idea what's goin' on right now...[/small]
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top