Simple confirm password or email validation

If you have a form with a ‘confirm password’ field or ‘confirm email’, and you don’t want to use a heavy validation plugin just for a simple confirmation like this, you can always use some simple ‘old school’ javascript and the onblur event, so when the field loses focus triggers the validation function.

<script type="text/javascript">
	function confirmPass() {
		var pass = document.getElementById("pass").value
		var confPass = document.getElementById("c_pass").value
		if(pass != confPass) {
			alert('Wrong confirm password !');
		}
	}
</script>
<form id="form" action="" method="post">
    <label for="pass">Password</label>
    <input type="password" id="pass" class="text" name="your_pass" value=""/>
    <label for="c_pass">Confirm Password</label>
    <input type="password" id="c_pass" class="text" name="your_c_pass" value="" onblur="confirmPass()"/>
</form>

See a demo.

Comments

  1. written by: Caki on May 23, 2014 at 9:16 pm - Reply

    Thanks a lot

  2. written by: Hans on October 16, 2014 at 9:31 am - Reply

    tesat

  3. written by: Alex on February 21, 2015 at 10:15 pm - Reply

    I think this is wrong confirmation, because it has a bug. When you have written right password and confirmation and then change first password input it does not has the reaction.

    • written by: Mihai on February 27, 2015 at 6:47 pm - Reply

      That happens, due to the fact that the ‘validation’ function is called when the ‘onblur’ event is triggered only on the ‘Confirm Password’ field.

      This is more like an example, not a real world password confirm validation.

  4. written by: Pankaj joshi on December 14, 2016 at 1:22 pm - Reply

    email and confirm email validation in php
    when email and confirm email value are same then data send to database otherwise not…
    how it is…plzz send code …

Leave a Reply

Your email address will not be published. Required fields are marked *

74 − = 70

This site uses Akismet to reduce spam. Learn how your comment data is processed.