Javascript to Match Two Fields

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.

Enter your email  
Enter your email again  


Here is the source code for this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<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>

Comments