creative creative - 6 months ago 13
Javascript Question

How to validate password with atleast one numeric, one Uppercase letter and 6-20 characters?

I have added this script to my form and its working with alpha numeric.The problem is when i click on wrong format on click function it will updated the data in database with submit button.How can i block? I need to update data with correct alert function.

function CheckPassword(inputtxt) {
var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
if (inputtxt.value.match(passw)) {
alert('Congratulations You have successfully changed your password!..Plz Logout and signin again.');
return true;
} else {
alert('Wrong Format...!')
return false;
}
}


<script src="check-password-2.js"></script>
<?php
if(isset($_POST['submit']) )
{
$password = $_POST['newpassword'];
$sql = mysql_query("UPDATE tbl_login SET str_password='$password',status='1' where str_username='$user'");
}
?>

<body onload='document.form1.newpassword.focus()'>
<form name="form1" action="" method="post" id="form1" onClick="CheckPassword(document.form1.newpassword)">
<table width="388" align="center"></br>
</<tr>
<td align="right">Enter Username</td>
<td align="center">
<input type="username" name="username" value="<?php echo ucwords($_SESSION['username']); ?> " class="TxtBox" disabled="disabled" />
</td>
</tr>
<h5>Input Password and Submit [6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter]</h5>
<tr>
<td align="right">New Password</td>
<td align="center">
<input type="password" name="newpassword" class="TxtBox" />
</td>
</tr>
<tr>
<td align="center">
<div align="center">
<input type="submit" name="submit" value="submit" />
</div>
</td>
</tr>
</table>
</form>
</body>

Answer

Firstly you need to use the onsubmit attribute of the form, not onclick. Secondly, you need to use a return statement within the attribute to provide the value returned by the function to the event handler to prevent or allow the form submission. You also need to fix several errors in your HTML. Try this:

<body onload='document.form1.newpassword.focus()'>
    <form name="form1" action="" method="post" id="form1" onsubmit="return CheckPassword(document.form1.newpassword)">
        <table width="388" align="center"></br>
            <tr>
                <td align="right">Enter Username</td>
                <td align="center">
                    <input type="username" name="username" value="<?php echo ucwords($_SESSION['username']); ?> " class="TxtBox" disabled="disabled" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <h5>Input Password and Submit [6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter]</h5>
                </td>
            </tr>
            <tr>
                <td align="right">New Password</td>
                <td align="center">
                    <input type="password" name="newpassword" class="TxtBox" />    
                </td>
            </tr>
            <tr>
                <td align="center">
                    <div align="center">
                        <input type="submit" name="submit" value="submit" />
                    </div>
                </td>
            </tr>
        </table>
    </form>
</body>