I want to take action if the values of the variables do not match in JavaScript.

Asked 1 years ago, Updated 1 years ago, 25 views

I have prepared two input tags for the password.If the value entered in the first input tag does not match the value of the second confirmation input tag when submit is pressed, I would like to write a code that says "Mismatch" in the alert.

HTML

<tr>
  <th>Password </th>
  <td><input type="password" th:text="${loginPass1}">/td> 
</tr>
<tr>
  <th>Password Confirmation</th>
  <td><input type="password" th:text="${loginPass2}">/td>
</tr>

JavaScript

var loginPass1 = 'loginPass';
var loginPass2 = 'loginPass';
// 
if(loginPass1!==loginPass2){
  alert('Passwords do not match');
}

javascript

2022-09-30 19:31

1 Answers

Perhaps you can do this.

 function passwordCheckFunction(){
  var loginPass1 = document.getElementById("loginPass1").value;
  var loginPass2 = document.getElementById("loginPass2").value;
  
  if(loginPass1!==loginPass2){
    alert("Passwords do not match.")
  }
}
<tr>
  <th>Password </th>
  <td><input type="password" id="loginPass1" th:text="${loginPass1}">/td> 
</tr>
<tr>
  <th>Password Confirmation</th>
  <td><input type="password" id="loginPass2" th:text="${loginPass2}">/td>
</tr>
<button type="button"class="passwordCheck"onclick="passwordCheckFunction()">Click Me!</button>


2022-09-30 19:31

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.