I check the contents of the input field for regular expression:

$('#inputMail').blur(function() { var pattern = /^([a-z0-9_\.-])+@[a-z0-9-]+\.([az]{2,6}\.)?[az]{2,6}$/i; if(($('#inputMail').val() != 0)&&(!pattern.test($('#inputMail').val()))) { alert('Вы ввели некорректный e-mail'); $('#inputMail').focus(); } 

In Chrome and Opera works great. Not in IE and Firefox. How to make cross-browser?

  • What is it for? ((( - humster_spb
  • To that there is a native email validation in html. - Visman
  • I don't need native validation, I need cross-browser validation with jquery tools - humster_spb
  • I recommend that you consider using the jQuery Validation library. It is supported by many browsers, there is a check on the validity of the email address. Save a lot of time and nerves. - Mergasov

2 answers 2

Do not use regular expressions to validate email addresses

To check the validity of an e-mail address, it is enough to check if the @ character is present in the string with other characters on the sides only once :

 let isValid = ($('#inputMail').val().match(/.+?\@.+/g) || []).length === 1; 

It is a bad idea to check email with more complicated regulars. An email address can contain not only [a-zA-Z0-9-_.] , But also an incredible cloud of other characters . There are regular expressions compiled to specification , but because of their length and complexity they are unsupported. Moreover, your regular @test.longdomainname.ru users of the @i.ua , @test.longdomainname.ru and many other variations of the boxes, because domains can be either one character long or 253 characters long.

Use type=email even if cross-browser compatibility is important.

As you indicated by @Visman , you should use type=email :

 <input type="email" required> 

This attribute is very important for mobile devices. When you click on this input field, the user in many mobile browsers will change the keyboard, which will make it easier for them to enter their email address. The second important function of this attribute is some kind of validation of email. The submit event will not work if the browser considers the entered email invalid. And browsers that do not support this attribute value will work with the field as with type=text .

Result

The end result should look something like this:

 $('#registration input[type=email]').on('blur', function () { let email = $(this).val(); if (email.length > 0 && (email.match(/.+?\@.+/g) || []).length !== 1) { console.log('invalid'); alert('Вы ввели некорректный e-mail!'); } else { console.log('valid'); alert('Вы ввели корректный e-mail!'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="registration"> <input type="email" required> </form> 

JSFiddle: jsfiddle.net/terron/4r8wyuop/

  • "Does the line contain the @ character with other characters on the sides only once?" - well, for example, dfgjh @ dfhg is the correct address? obviously not. although the @ symbol with other characters on the sides is present and only once. at least still on the point should be checked. and the length of the top-level domain would also be nice. - humster_spb
  • one
    @humster_spb, yes, valid . - neluzhin
  • And the length of the top-level domain is not necessary to check. From a technical point of view, they can be one character long (but at the moment the minimum available length is 2 characters), or into a whole word, for example, mitsubishi . They are added quite actively and it would be foolish to limit the length of the first-level domain, because you do not know in advance what domain zone can add. - neluzhin
  • Well, in a sense - valid? No letter will arrive on dfgjh @ dfhg, because this is not an address, but an address stub. at least at the end there should still be a point and top-level domain. What are you trying to convince me of? )) in that it is not necessary to check? -) I think that it is necessary to - if there is a desire to receive an e-mail, and not a character set with @ in the middle. - humster_spb
  • @humster_spb, have you watched my screenshot? Open from Android, iOS or any Linux http://to/ and you will see a miracle. - neluzhin

I think this is $('#inputMail').val() does not return 0, but an empty string.

 $('#inputMail').blur(function() { var pattern = /^([a-z0-9_\.-])+@[a-z0-9-]+\.([az]{2,6}\.)?[az]{2,6}$/i; if($(this).val() && !pattern.test($(this).val()) { alert('Вы ввели некорректный e-mail'); $(this).focus(); } }); 
  • No, nothing has changed. I think the thing here is in pattern.test ($ (this) .val () - IE and Firefox do not understand what this means. Somehow you can write a test differently? - humster_spb 1616