<script type="text/javascript">
function checkEmail() {
var mail1=document.getElementById('email1');
var mail2=document.getElementById('email2');
var err=document.getElementById('error');
if ((mail1.value != mail2.value)&&(mail1.value!="")&&(mail2.value!="")){
mail1.style.background="#FF7B7B";
mail2.style.background="#FF7B7B";
err.innerHTML="The email id's do not match"
return false;
}
if((mail1.value=="")||(mail2.value==""))
{
//do nothing
}
else {
mail1.style.background="#A2FFA2";
mail2.style.background="#A2FFA2";
err.innerHTML=""
return true;
}
}
</script>
Here i am going to show one example to match two fields using java script without using submit button.
Here I will make use "onblur" event.
<div>
Enter your email <input type="email" id="email1" >
<br>
Enter your email again <input type="email" id="email2" onblur="checkEmail()" >
<div id="error" style="color:#ff0000"></div>
</div>
<br>