10

Referring to this issue:

How can I set a minimum length for a field with jQuery?,

<form id="new_invitation" class="new_invitation" method="post" data-remote="true" action="/invitations" accept-charset="UTF-8">
    <div id="invitation_form_recipients">
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_0"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_1"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_2"><br>
        <input type="text" value="" name="invitation[recipients][]" id="invitation_recipients_3"><br>
    </div>
    <input type="submit" value="Send invitation" name="commit">
</form>

What would the code be for settting a minimum length for a field with jQuery?

$('#new_invitation').submit(function(event) {
    if ($('#invitation_form_recipients input').filter(function() {
        return $(this).val();
    }).length == 0) {
        // All the fields are empty
        // Show error message here

        // This blocks the form from submitting
        event.preventDefault();
    }
});

How can I validate that every field input have a valid email address with jQuery? In the above code?

5

6 Answers 6

41

You probably want to use a regex like the one described here to check the format. When the form's submitted, run the following test on each field:

var userinput = $(this).val();
var pattern = /^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i

if(!pattern.test(userinput))
{
  alert('not a valid e-mail address');
}​
3
  • 1
    I am trying this, but its not working, i have just written- [email protected]. <input type="text" class="form-control" id="manager_email" placeholder="Email Address" value="" pattern="/^\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b$/i" required /> Anyone has some idea about this. Commented Jun 8, 2017 at 11:46
  • If you're using an input pattern, be sure not to surround it with forward slashes (you may need to manually add lowercase characters to the pattern to allow case-insensitive input). pattern="^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
    – rjz
    Commented Jun 8, 2018 at 16:48
  • 5
    The above pattern does not validate correct email addresses as specified in RFC5322 and is misleading. If you use this on a live website you will block valid users. special characters are allows int he local part of the address !#$%&'*+-/=?^_`{|}~ please see stackoverflow.com/questions/2049502/… and tools.ietf.org/html/rfc5322
    – nick fox
    Commented Aug 31, 2018 at 17:04
7

This regex can help you to check your email-address according to all the criteria which gmail.com used.

var re = /^\w+([-+.'][^\s]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

var emailFormat = re.test($("#email").val()); // This return result in Boolean type

if (emailFormat) {}
2
  • 2
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    – DimaSan
    Commented Dec 20, 2016 at 12:50
  • @DimaSan Thanks for your valuable advice. Commented Dec 26, 2016 at 6:07
1
Email: {
                      group: '.col-sm-3',
                      enabled: false,
                      validators: {
                          //emailAddress: {
                          //    message: 'Email not Valid'
                          //},
                          regexp: {
                              regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
                              message: 'Email not Valid'
                          },
                      }
                  },
1

This : /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i is not working for below Gmail case

[email protected] [email protected]

Below Regex will cover all the E-mail Points: I have tried the all Possible Points and my Test case get also pass because of below regex

I found this Solution from this URL:

Regex Solution link

/(?:((?:[\w-]+(?:\.[\w-]+)*)@(?:(?:[\w-]+\.)*\w[\w-]{0,66})\.(?:[a-z]{2,6}(?:\.[a-z]{2})?));*)/g
0

This :

 var email = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
2
-2
function mailValidation(val) {
    var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

    if (!expr.test(val)) {
        $('#errEmail').text('Please enter valid email.');
    }
    else {
        $('#errEmail').hide();
    }
}
1
  • 1
    As noted by Rick Fox in other comments, the _ and a lot of other bizarre characters are allowed before the @ character. Actually, the _ is fairly common. Commented Jul 28, 2019 at 19:26

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