|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi I have a form to register new user in which i ask them to enter their passwords twice. I want them to enter passwords greater than or equal to six characters and also they should enter same password, but if they don't then the onBlur function should give appropiate messages to the user. I have written the following code but it is not working can anyone of you help me with this code.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Registration Form for New User</title> <style type="text/css"> <!-- .style1 { font-family: "Monotype Corsiva"; font-weight: bold; } .style2 {font-family: "Monotype Corsiva"} body { background-image: url(); } --> </style> <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript"> function validate(new_user) { if ( isName() && isDept() && isBuild() && isEmail() && isPhone()) { document.forms[0].action = "new_user.asp";document.forms[0].submit(); } return true; } //Name validator function isName(new_user) { var str = document.forms[0].username.value; if (str == "") { alert("\nThe Name field is blank .\n\nPlease re-enter your Name."); document.forms[0].username.focus(); return false; } if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z")) { alert("The Name should begin with an alphabetic character."); document.forms[0].username.select(); return false; } for (var i = 1; i < str.length; i++) { var ch = str.substring(i, i + 1); if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '')) { alert("\nThe Name field accepts letters,numbers & underscore only.\n\nPlease re-enter your Name."); document.forms[0].username.select(); document.forms[0].username.focus(); return false; } } return true; } // Department Validator function isDept(new_user) { var str = document.forms[0].department.value; if (str == "") { alert("\nThe Department field is blank .\n\nPlease re-enter your Department."); document.forms[0].department.select(); document.forms[0].department.focus(); return false; } return true; } // Building Validator function isBuild(new_user) { var str = document.forms[0].building.value; if (str == "") { alert("\nThe Building field is blank .\n\nPlease re-enter your Building."); document.forms[0].building.select(); document.forms[0].building.focus(); return false; } return true; } // Email Address Validator function isEmail(new_user) { var str = document.forms[0].email.value; if (str == "") { alert("\nThe Email Address field is blank .\n\nPlease re-enter your Email Address."); document.forms[0].email.select(); document.forms[0].email.focus(); return false; } return true; } // Phone Validator function isPhone(new_user) { var str = document.forms[0].phone.value; if (str == "") { alert("\nThe Phone field is blank .\n\nPlease re-enter your Phone Number."); document.forms[0].phone.select(); document.forms[0].phone.focus(); return false; } return true; } // Password Validator function isPass(new_user) { var str = document.new_user.password.value; if ((str == "") || (str.length < 6)) { alert("\nThe PASSWORD field is either empty or less than 6 characters.\n\nPlease re-enter your Password.") document.new_user.password.focus(); return false; } var str1 = document.new_user.password1.value; if (str != str1) { alert("Passwords typed do not match, please re-enter your passwords.\n\n"); document.new_user.password.select(); document.new_user.password.focus(); document.new_user.password1.select(); document.new_user.password1.focus(); return false; } return true; } </SCRIPT> </head> <body onLoad="window.document.new_user.username.focus();" > <center> <form name="new_user" method="post" > <table width="50%" border="1" align="center" cellpadding="0.5" cellspacing="0.5"> <tr> <td width="391"><h1 align="center" class="style1"><img src="register.gif" height=100 width=150></h1> <br> <br> <h4 align="center"><font color="red"><b><i>All fields marked with</i> * <i>are mandatory</i> </b></font></h4> <table name="userdetails" align="center" cellspacing=5 cellpadding=5> <tr align="left"> <td><span class="style2">Username:</span></td> <td><input type="text" name="username"><font color="red">*</font></td> </tr> <tr align="left"> <td><span class="style2">Password:</span></td> <td><input type="password" name="userpassword"><font color="red">*</font></td> </tr> <tr align="left"> <td><span class="style2">Retype Password:</span></td> <td><input type="password" name="userpassword1" onBlur="return isPass(new_user)"><font color="red">*</font></td> </tr> <tr align="left"> <td></td> <td><span class="style2"><font color="red">Password must be at least 6 characters long. It may contain alphabets A-Z, or a-z, numbers 0-9 and underscore but no spaces and special characters.</font></span></td> </tr> <tr align="left"> <td><span class="style2">Room Number:</span></td> <td><input type="text" name="roomno"></td> </tr> <tr align="left"> <td><span class="style2">Department:</span></td> <td><input type="text" name="department"><font color="red">*</font></td> </tr> <tr align="left"> <td><span class="style2">Building:</span></td> <td><input type="text" name="building"><font color="red">*</font></td> </tr> <tr align="left"> <td><span class="style2">E-mail:</span></td> <td><input type="text" name="email"><font color="red">*</font></td> </tr> <tr align="left"> <td><span class="style2">Phone Number:</span></td> <td><input type="text" name="phone"><font color="red">*</font></td> </tr> </table> <p><br> <br> <input type="button" align="absmiddle" value="REGISTER ME" onclick="return validate()"> <input type="reset" align="absmiddle" value="CLEAR FORM"> </p> <p> </p></td> </tr> </table> <br> </form> </center> <br> <br> </body> </html> |
|
#2
|
||||
|
||||
|
There were a couple of issues with your code.
Here is an example Code:
<script language="JavaScript">
function validatePasswords()
{
var pass1 = document.forms[0].password1.value;
var pass2 = document.forms[0].password2.value;
// Validate the first password field
if (pass1 == '' || pass1.length < 6)
{
alert("\nThe FIRST PASSWORD field is either empty or less than 6 characters.\n\nPlease re-enter first password.");
document.forms[0].password1.select();
document.forms[0].password1.focus();
return false;
}
if(pass2 == '' || pass2.length < 6)
{
alert("\nThe SECOND PASSWORD field is either empty or is less than 6 characters.\n\nPlease re-enter the second password.");
document.forms[0].password2.select();
document.forms[0].password2.focus();
return false;
}
if (pass2 != pass1)
{
alert("Your passwords do not match, please verify you entered the same password in each field.\n\n");
document.forms[0].password1.select();
document.forms[0].password1.focus();
return false;
}
return true;
}
</script>
<form name="form1">
<table>
<tr align="left">
<td><span class="style2">Password:</span></td>
<td><input type="password" name="password1"><font color="red">*</font></td>
</tr>
<tr align="left">
<td><span class="style2">Retype Password:</span></td>
<td><input type="password" name="password2" onBlur="return validatePasswords()"><font
color="red">*</font></td>
</tr>
</table>
</form>
|
![]() |
| Viewing: ASP Free Forums > Other > Programming Help > Javascript onBlur error |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|