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?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • 3
    possible duplicate of [Validate email with jQuery](http://stackoverflow.com/questions/5778624/validate-email-with-jquery), [jQuery Email Regex](http://stackoverflow.com/questions/8112329/jquery-email-regex), [What regular expression does jQuery use for their email validation?](http://stackoverflow.com/questions/8424980/what-regular-expression-does-jquery-use-for-their-email-validation), [Validate email address in Javascript?](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript), [and more...](http://stackoverflow.com/search?q=javascript+validate+email) – Wesley Murch Mar 05 '12 at 18:56
  • 3
    generally concerning difficulty of email validation via RegEx http://stackoverflow.com/q/156430/925580 – Lucius Mar 05 '12 at 19:00
  • 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 in the local part of the address !#$%&'*+-/=?^_`{|}~ please see https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address and https://tools.ietf.org/html/rfc5322 – nick fox Aug 31 '18 at 17:05
  • Wait, so is the accepted answer what you went with, or the "The Solution!" part in your question? – General Grievance Dec 23 '21 at 13:13
  • 2
    Does this answer your question? [What's the best way to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/whats-the-best-way-to-validate-an-email-address-in-javascript) – Robert Dec 23 '21 at 16:58

6 Answers6

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');
}​
Community
  • 1
  • 1
rjz
  • 16,182
  • 3
  • 36
  • 35
  • 1
    I am trying this, but its not working, i have just written- abc@gmail.com. `` Anyone has some idea about this. – Mukesh Joshi Jun 08 '17 at 11:46
  • If you're using an input `pattern`, be sure not to [surround it with forward slashes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input) (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 Jun 08 '18 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 https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address and https://tools.ietf.org/html/rfc5322 – nick fox Aug 31 '18 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) {}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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 Dec 20 '16 at 12:50
  • @DimaSan Thanks for your valuable advice. – Aditi_Systematix Dec 26 '16 at 06:07
1
Email: {
                      group: '.col-sm-3',
                      enabled: false,
                      validators: {
                          //emailAddress: {
                          //    message: 'Email not Valid'
                          //},
                          regexp: {
                              regexp: '^[^@\\s]+@([^@\\s]+\\.)+[^@\\s]+$',
                              message: 'Email not Valid'
                          },
                      }
                  },
ARC
  • 1,061
  • 14
  • 33
1

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

gmail.@gmail.com gmail@.gmail.com

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}$/;
Fahimeh Ahmadi
  • 813
  • 8
  • 13
-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();
    }
}
Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Sakthivel
  • 26
  • 2
  • 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. – Mark Stewart Jul 28 '19 at 19:26