-1

I need 2 compare values from 2 textboxes. If the comparison differs only one character then I need to show an error message. For example textbox1 contains abcde and textbox2 contains abcge then show alert that "textbox1 value nearly equal to textbox2 value". Need to write this using Javascript. Is there any logic to implement this?

  • 3
    Yes. There is a way. – VDWWD Jan 20 '20 at 07:12
  • like a confirm password kind of functionality so the two text box should contain same string , is that what you are looking for ? – Learner Jan 20 '20 at 07:14
  • I need to check current password and new password. The new password must be more than one character differ from the previous password – Aishwarya Snigdha Jan 20 '20 at 07:17
  • so where will you store the current password in the field or inside the current context ? – Learner Jan 20 '20 at 07:21
  • as shown in the below answer you can use the same logic only think is you need to add the current value there inside password field and make it disabled because if you dont want to change, or else if the user enters it you can just use the same below logic in the answer and change the label from password to current password, i hope that will solve the issue ? – Learner Jan 20 '20 at 07:23
  • Thanks. That code compares 2 values but I need to check that new password is more than one character differ from current passoword. For example if new password is abcfg (current password abcde) then it is acceptable – Aishwarya Snigdha Jan 20 '20 at 07:44

1 Answers1

1

Check this one, here we are using onkeyup to both input fields.

var check = function() {
  if (document.getElementById('password').value ==
    document.getElementById('confirm_password').value) {
    document.getElementById('message').style.color = 'green';
    document.getElementById('message').innerHTML = 'matching';
  } else {
    document.getElementById('message').style.color = 'red';
    document.getElementById('message').innerHTML = 'not matching';
  }
}
<label>password :
  <input name="password" id="password" type="password" onkeyup='check();' />
</label>
<br>
<label>confirm password:
  <input type="password" name="confirm_password" id="confirm_password"  onkeyup='check();' /> 
  <span id='message'></span>
</label>
Learner
  • 8,379
  • 7
  • 44
  • 82