[英]How to submit only when 2 text fields match?
我正在实现此代码: https://www.geeksforgeeks.org/password-matching-using-javascript/
我希望仅在两个密码匹配时才发生提交操作。 但是如果我将字段留空或密码不匹配,就会出现问题。 无论如何它都会执行操作提交,那么我如何强制它仅在两个输入匹配且不为空时才执行它?
这是代码:
<!DOCTYPE html>
<html>
<head>
<script>
// Function to check Whether both passwords
// is same or not.
function checkPassword(form) {
password1 = form.password1.value;
password2 = form.password2.value;
// If password not entered
if (password1 == '')
alert ("Please enter Password");
// If confirm password not entered
else if (password2 == '')
alert ("Please enter confirm password");
// If Not same return False.
else if (password1 != password2) {
alert ("\nPassword did not match: Please try again...")
return false;
}
// If same return True.
else{
alert("Password Match: Welcome to GeeksforGeeks!")
return true;
}
}
</script>
</head>
<body>
<form onSubmit = "return checkPassword(this)" action="success.php" method="POST">
<table border = 1 align = "center">
<tr>
<!-- Enter Password. -->
<td>Password:</td>
<td><input type = password name = password1></td>
</tr>
<tr>
<!-- To Confirm Password. -->
<td>Confirm Password:</td>
<td><input type = password name = password2></td>
</tr>
<tr>
<td colspan = 2 align = right>
<input type = submit value = "Submit"></td>
</tr>
</table>
</form>
</body>
</html>
success.php
了。php 就是这个
<?php
echo("SUCCESS");
?>
如果其中一个密码为空,则需要返回 false
// If password not entered
if (password1 == '') {
alert ("Please enter Password");
return false;
}
// If confirm password not entered
if (password2 == '') {
alert ("Please enter confirm password");
return false;
}
这是你想写的
if (password == '') {
alert ("Please enter the Password");
return false;
}
else (password == '') {
alert ("Please enter confirm the password");
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.