kas Posted May 23, 2008 Share Posted May 23, 2008 I need code that includes a while{} construct to ensure that a user enters a valid password into a prompt dialogue box. Valid passwords include at least 6 characters and begin with the number 5. The validation should be triggered by the click event of the commend button cmdValidatePassword that should call the function validatePassword(). Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 23, 2008 Share Posted May 23, 2008 Use regular expressions: http://www.w3schools.com/JS/js_obj_regexp.asp The regexp you would use would be something like: /5[a-zA-Z]{5}/ Your while construct would be something like: var pass = ""; do{ // not sure if i'm using prompt() correctly, look it up online (maybe confirm?) pass = prompt( "Enter a password" ); }while( /* pass fails regexp */ ); // pass is now valid, do whatever Quote Link to comment Share on other sites More sharing options...
kas Posted May 23, 2008 Author Share Posted May 23, 2008 That would be simplier but its actually for my coursework. Here is what I have. Any suggestions ? <script language="javascript"> function cmdValidatePassword() { var strPassword=(prompt("Validate Password", "Enter Password")); //This will prompt you for the password while (strPassword.length < 6 || strPassword.charAt(0)!=5) // this sets the password to start with 5 and must be at least 6 characters long { strPassword=(prompt("Validate Password", "Enter Password")); } alert("Password Valid"); //The alert notifies you that a valid password has been entered } </script> Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 23, 2008 Share Posted May 23, 2008 Put double quotes around 5 so that it's a character and not a number. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.