-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?

12
  • How does your form and its submit look like?
    – Dario
    Apr 18, 2019 at 9:30
  • This is actually in a jsp page. So i am not using form though
    – Daredevil
    Apr 18, 2019 at 9:31
  • Validation only kicks in if you use a form and onsubmit. Your onclick will always fire, regardless of the validity of the input. Apr 18, 2019 at 9:31
  • So input type=email is useless in my case?
    – Daredevil
    Apr 18, 2019 at 9:32
  • no, its still usefull, it helps to display a different keyboard on mobiles. You should wrap your inputs around a form to get the validation error. Apr 18, 2019 at 9:33

2 Answers 2

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;
    }
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();
     }
}

4
  • what does this do?
    – Daredevil
    Apr 18, 2019 at 9:46
  • validation for your email field !
    – Javed
    Apr 18, 2019 at 10:13
  • is there anything that i can change the reg? Because I can send to multiple recipient like this[email protected],[email protected] and it will say it's not valid
    – Daredevil
    Apr 18, 2019 at 10:13
  • @Daredevil then u need to "split()" your input value and on every split run a regex on it.
    – Javed
    Apr 18, 2019 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.