Click here to Skip to main content
14,978,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on MVC 5, there I want to insert two Email Id inside textbox with comma seperated and also should allow only 2 or 1 emailids like below image shown.

[^]

What I have tried:

I have tried jquery code on submit button click,

JavaScript
submitPrimUserAllReg = {  
    submitUser: function () {  
        debugger;  
        if (!$('#hdTurnOver').val() == '' && !$('#hdTurnOver').val() == '') {  
            var emailid = $('#userEmailid').val().split(",");  
            var validemail = [];  
            $.each(emailid, function (i) {  
  
                if (!isEmail(emailid[i])) {  
                    alert('Please Enter Valid EmailID');  
                    return false;  
                }  
                else {  
                    validemail.push(emailid[i]);  
                    var validemailcomma = validemail.join(",");  
                    if (!validemailcomma == '') {  
  
                        var validemailsplited = validemailcomma.split(",").length;  
                        if (validemailsplited == 2 || validemailsplited == 1) {  
                            $('#frmSubmitPremUserReg').submit();  
                            //alert('success!')  
                        }  
                        else {  
                            alert('Only two EmailID');  
                            return false;  
                        }  
                    }  
                }  
            });  
        }  
        else {  
            return false;  
        }  
    }  
}  


After using this code, there is an issue: If the emailId enter like "test1@gmail.com,test " then the form is submitting. But here I want to show alert message like 'Please Enter Valid EmailID'. How can I solve this?

Please help me...
Posted
Updated 28-Feb-17 21:50pm

1 solution

try this

var emailid = $('#userEmailid').val().split(",");
       if (emailid.length <= 2) {
           for (var i = 0; i < emailid.length; i++) {
               if (!isEmail(emailid[i])) {
                   alert('Please Enter Valid EmailID');
                   return false;
               }
           }
           // valid email id
           $('#frmSubmitPremUserReg').submit();
       }
       else {
           alert('Max 2 email id allowed.');
           return false;
       }
   
v2

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