我正在为我的网站创建和注册表格,并且试图添加一个JS脚本,该脚本比较两个密码输入(密码和重复密码),然后显示一条消息“密码不相同”。这是我的代码:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="margin-top: 5px; margin-bottom: 5px">
<h5 class="modal-title" id="exampleModalLabel" style="text-decoration: underline;text-align: center; font-size: 25px; line-height: 20px">Zarejestruj sie ! </h5>
</div>
<div class="modal-body" style="margin-top: 10px">
<form>
<div class="form-group">
<form>
<div class="form-group">
<label>New Login</label>
<input type="text" class="form-control" placeholder="your login">
</div>
<div class="form-group">
<label>New password</label>
<input type="password" class="form-control" id="pas1" placeholder="password">
</div>
<div class="form-group">
<label>Repeat password</label>
<input type="password" class="form-control" id="pas2" placeholder="password">
</div>
<p id="alert"></p>
</form>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Zamknij</button>
<button type="submit" class="btn btn-primary" onclick="Funkcja()">Zarejestruj konto</button>
</div>
</div>
</div>
</div>
在hmtl代码下面,我放置了这个JS脚本:
<script>
function Chceck() {
var x, y, text;
// Get the value of the input field with id="numb"
x = document.getElementById("pas1").value;
y = document.getElementById("pas2").value;
// If x is Not a Number or less than one or greater than 10
if (x = y) {
text= "";
} else {
text = "Hasla nie sa takie same";
}
document.getElementById("alert").innerHTML = text;
}
</script>
有人可以帮我实现它吗?
答案 0 :(得分:1)
使用==
或===
进行两个变量的比较,使用=
进行赋值:
if (x == y) { // change this line to be 'x == y' from 'x = y'
text= "";
} else {
text = "Hasla nie sa takie same";
}
您可以认为==
的严格程度要比===
例如1 == true
是true
语句,而1 === true
是false
语句
答案 1 :(得分:0)
x = y
将y
的值分配给x
,因此它将始终为真。您应该使用x==y
(或x====y
)进行比较。比较变量时,应使用===
不强制类型转换。例如,如果您比较1=="1"
,则为true,但是1==="1"
为false。以下代码应该起作用:
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header" style="margin-top: 5px; margin-bottom: 5px">
<h5 class="modal-title" id="exampleModalLabel" style="text-decoration: underline;text-align: center; font-size: 25px; line-height: 20px">Zarejestruj sie ! </h5>
</div>
<div class="modal-body" style="margin-top: 10px">
<form>
<div class="form-group">
<form>
<div class="form-group">
<label>New Login</label>
<input type="text" class="form-control" placeholder="your login">
</div>
<div class="form-group">
<label>New password</label>
<input type="password" class="form-control" id="pas1" placeholder="password">
</div>
<div class="form-group">
<label>Repeat password</label>
<input type="password" class="form-control" id="pas2" placeholder="password">
</div>
<p id="alert"></p>
</form>
<button type="submit" class="btn btn-primary" onclick="Chceck(event)">Zarejestruj konto</button>
</div>
</div>
</div>
</div>
<script>
function Chceck(event) {
var x, y, text;
// Get the value of the input field with id="numb"
x = document.getElementById("pas1").value;
y = document.getElementById("pas2").value;
//check if x and y are both not empty
if(x.trim().length&&y.trim().length){
if (x == y) {
text= "";
} else {
text = "Hasla nie sa takie same";
event.preventDefault();//prevent default submission of form
}
document.getElementById("alert").innerHTML = "<b style='color: red;'>"+text+"</b>";
} else {
event.preventDefault();//prevent default submission of form
document.getElementById("alert").innerHTML = "<b style='color: red;'>Password can not be empty!</b>";
}
}
</script>