Jump to content

Password Validation


kas

Recommended Posts

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().

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.