我已将此脚本添加到我的表单并使用字母数字。问题是当我点击单击功能上的错误格式时,它将使用提交按钮更新数据库中的数据。如何阻止?我需要使用正确的警报功能更新数据。
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>
答案 0 :(得分:0)
首先,您需要使用onsubmit
的{{1}}属性,而不是form
。其次,您需要在属性中使用onclick
语句,以将函数返回的值提供给事件处理程序,以防止或允许表单提交。您还需要修复HTML中的多个错误。试试这个:
return