Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have two textboxes
Primary email and secondary email
here I need validation when I enter same email in both fields?

How can I implement this in javascript?

please share me code samples
Posted
Comments
Akinmade Bond 26-Mar-14 2:03am    
What do you want to do? Check if the primary email is different from the secondary email? Check if both fields contain valid emails? Or both?

Here's what you should do.

Before your form submits, assuming you are using JQuery based on your tag.


JavaScript
$('form').on('submit',function() {
    var pmail = $('#pmail').val().trim()
    var smail = $('#smail').val().trim()

    //Check if there are values in either textbox
    //Otherwise show an alert and <code>return false;</code>

    //Check if the primary email and secondary email are valid emails, see solution 1.
    //Otherwise return false.

    //Check if primary email and secondary email are the same.
    if ( pmail === smail ) {
        alert('Please enter an address different from your primary email in the secondary email');
        return false;
    }
});

HTML
<form action="/action" id="form-id">
    <label for="pmail">Primary Email:
        <input type="email" name="pmail" id="pmail" placeholder="example@example.com">
    </label>
    <label for="smail">Secondary Email:
        <input type="email" name="smail" id="smail" placeholder="another@example.com">
    </label>
    <input type="submit" value="Submit Form">
</form>



Note the use of the === operator to check for comparison.
 
Share this answer
 
v5
JavaScript Email Address validation using Regular Expression
[^]


Put if condition if both textboxes values is equal then execute your code otherwise put alert "Email ID doesn't match"
 
Share this answer
 
v2
JavaScript
if($('#primaryemail').val()==$('#secondaryemail').val()) {
     $('#Error_pspan').html("primary email and secondary email cannot  be same");
     $('#secondaryemail').focus();                 
    }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900