-1

I have a textbox of input type email because it's meant for email addresses. So my page looks like this:

enter image description here

The input type of email should handle the syntax of email.So if email is test@@@gmail.com,it should not gone through though. When I hit send, it still able to initiate the email.

My javascript function:

<script>
    //function to send email

    function sendmessage(){

        var recipient = document.getElementById("recipient").value;

        var subject = document.getElementById("subject").value;

        var content=document.getElementById("content").value;



        $.ajax({

            url: 'sendemail.jsp',
            type: 'POST',
            data: {
                recipient:recipient,
                subject:subject,
                content:content

            },

            success: function (data) {
                alert("Successfully initiated email to queue");
            },
            error: function (request, error) {
                alert("Request: " + JSON.stringify(error));
            }
        });

    }



</script>

<body>

<div class="email_font">
&emsp;&emsp; To:<input type="email" style="font-size: 10pt;" size="70" id="recipient"><br><br>

        Subject:<input type="text" style="font-size: 10pt" size="70" id="subject" ><br><br>



Content:<br><textarea cols="80"  rows="10" id="content"  style="font-size: 13pt;">
    <%=files%>:   <%=url%>


</div>

<div class="Send">
    <button type="button" style="font: 13px/1.231 Trebuchet MS;" onclick="sendmessage()"> Send </button>
</div>

Clicking send still allows to send, is there anything I made wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Daredevil
  • 1,672
  • 3
  • 18
  • 47

2 Answers2

2

You can Split the emails on a comma and validate the entries one by one Following is the code to accept multiple valid email id both comma and semicolon as separator

  function validateEmails(emailString) {
        var regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var result = emailString.replace(/\s/g, "").split(/,|;/);        
        for(var i = 0;i < result.length;i++) {
            if(!regex.test(result[i])) {
                return false;
            }
        }       
        return true;
    }
Ajinkya
  • 325
  • 3
  • 12
0

Use the following code and it should help:

HTML:

<div class="input-wrp">
    <input type="email" name="email" placeholder="E-MAIL ID*" required>
    <p class="error">Email Error</p>
</div>

JS:

$('input[type="email"]').on('input', function() {
    email($(this));     //Function Call
});

function email(input) {           //Function
    var inputVal = input.val();
    var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if(!reg.test(inputVal) && inputVal.length > 0) {
        input.parent().find('.error').show();
     } else {
        input.parent().find('.error').hide();
     }
}

Javed
  • 253
  • 3
  • 5