admin管理员组文章数量:1026989
I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window
This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You
<body>
<form>
Password:<br>
<input type="password" name="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" name="confirmPassword" placeholder="Confirm password">
<br>
<input type="submit" onclick="ansValidation()" value="Sign Up">
</form>
<br>
<script>
function ansValidation() {
var nameValue = document.getElementById("name")
var passValue = document.getElementById("password")
var confpassValue = document.getElementById("confirmPassword")
if(typeof nameValue != String){
window.alert("Please re-enter your name")
}else if(passValue != confpassValue) {
window.alert("Passwords do not match!")
}
}
</script>
</body>
I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window
This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You
<body>
<form>
Password:<br>
<input type="password" name="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" name="confirmPassword" placeholder="Confirm password">
<br>
<input type="submit" onclick="ansValidation()" value="Sign Up">
</form>
<br>
<script>
function ansValidation() {
var nameValue = document.getElementById("name")
var passValue = document.getElementById("password")
var confpassValue = document.getElementById("confirmPassword")
if(typeof nameValue != String){
window.alert("Please re-enter your name")
}else if(passValue != confpassValue) {
window.alert("Passwords do not match!")
}
}
</script>
</body>
Share
Improve this question
asked Nov 11, 2019 at 15:22
codingcodfishcodingcodfish
31 silver badge2 bronze badges
2
-
4
You have to get the "value" properties of the
<input>
elements – Pointy Commented Nov 11, 2019 at 15:23 -
1
console.log(passValue, confpassValue)
I doubt two different input elements will ever be equal. – epascarello Commented Nov 11, 2019 at 15:25
1 Answer
Reset to default 4The document.getElementById
function returns a node, not the value from the input. You need to call the value
property on these nodes to access their values.
function ansValidation(ev) {
ev.preventDefault
// there is no input named name
//var nameValue = document.getElementById("name").value
var nameValue = "test";
var passValue = document.getElementById("password").value
var confpassValue = document.getElementById("confirmPassword").value
// the typeof operator returns a string.
if(typeof nameValue !== "string"){
window.alert("Please re-enter your name")
// we use strict validation ( !== ) because it's a good practice.
}else if(passValue !== confpassValue) {
window.alert("Passwords do not match!")
}
}
<form>
Password:<br>
<input type="password" id="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" id="confirmPassword" placeholder="Confirm password">
<br>
<input type="button" href="#" onclick="ansValidation(event)" value="Sign Up">
</form>
I have also edited your code to make it work in the snippet. Here are some of the problem it had.
- You need to pass an
id=""
attribute to your inputs if you want to use thegetElementById
function - the input
name
did not exists - the operator
typeof
returns a string, you need to pare that to a string. - When paring things in Javascript, it is always a good practice to strictly pare things (
===
). - I've added the event to the function and the
ev.preventDefault
to make sure the validation is done correctly before sending the form.
I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window
This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You
<body>
<form>
Password:<br>
<input type="password" name="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" name="confirmPassword" placeholder="Confirm password">
<br>
<input type="submit" onclick="ansValidation()" value="Sign Up">
</form>
<br>
<script>
function ansValidation() {
var nameValue = document.getElementById("name")
var passValue = document.getElementById("password")
var confpassValue = document.getElementById("confirmPassword")
if(typeof nameValue != String){
window.alert("Please re-enter your name")
}else if(passValue != confpassValue) {
window.alert("Passwords do not match!")
}
}
</script>
</body>
I'm trying to make a simple password confirmer work in JS. Basically the user has 2 input fields, and if they match nothing happens, but if they dont match then there will be an alert window
This is also my first day doing javascript, and also my first question on Stack Overflow, apologies for any errors or mishaps. Thank You
<body>
<form>
Password:<br>
<input type="password" name="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" name="confirmPassword" placeholder="Confirm password">
<br>
<input type="submit" onclick="ansValidation()" value="Sign Up">
</form>
<br>
<script>
function ansValidation() {
var nameValue = document.getElementById("name")
var passValue = document.getElementById("password")
var confpassValue = document.getElementById("confirmPassword")
if(typeof nameValue != String){
window.alert("Please re-enter your name")
}else if(passValue != confpassValue) {
window.alert("Passwords do not match!")
}
}
</script>
</body>
Share
Improve this question
asked Nov 11, 2019 at 15:22
codingcodfishcodingcodfish
31 silver badge2 bronze badges
2
-
4
You have to get the "value" properties of the
<input>
elements – Pointy Commented Nov 11, 2019 at 15:23 -
1
console.log(passValue, confpassValue)
I doubt two different input elements will ever be equal. – epascarello Commented Nov 11, 2019 at 15:25
1 Answer
Reset to default 4The document.getElementById
function returns a node, not the value from the input. You need to call the value
property on these nodes to access their values.
function ansValidation(ev) {
ev.preventDefault
// there is no input named name
//var nameValue = document.getElementById("name").value
var nameValue = "test";
var passValue = document.getElementById("password").value
var confpassValue = document.getElementById("confirmPassword").value
// the typeof operator returns a string.
if(typeof nameValue !== "string"){
window.alert("Please re-enter your name")
// we use strict validation ( !== ) because it's a good practice.
}else if(passValue !== confpassValue) {
window.alert("Passwords do not match!")
}
}
<form>
Password:<br>
<input type="password" id="password" placeholder="Password">
<br>
Re-enter Password:<br>
<input type="password" id="confirmPassword" placeholder="Confirm password">
<br>
<input type="button" href="#" onclick="ansValidation(event)" value="Sign Up">
</form>
I have also edited your code to make it work in the snippet. Here are some of the problem it had.
- You need to pass an
id=""
attribute to your inputs if you want to use thegetElementById
function - the input
name
did not exists - the operator
typeof
returns a string, you need to pare that to a string. - When paring things in Javascript, it is always a good practice to strictly pare things (
===
). - I've added the event to the function and the
ev.preventDefault
to make sure the validation is done correctly before sending the form.
本文标签: How to confirm two passwords are equal in JavaScriptStack Overflow
版权声明:本文标题:How to confirm two passwords are equal in JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745627130a2159932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论