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
https://forums.phpfreaks.com/topic/106947-password-validation/
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
https://forums.phpfreaks.com/topic/106947-password-validation/#findComment-548219
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
https://forums.phpfreaks.com/topic/106947-password-validation/#findComment-548311
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.