$(document).ready(function() { var pattern = "\S+@[az]+.[az]+"; //var pattern="/^([a-z0-9_\.-])+@[a-z0-9-]+\.([az]{2,4}\.)?[az]{2,4}$/i" /* var pattern="/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[az][az])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i";*/ /*var pattern="/^([az\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[az\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][az\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[az\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][az\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[az\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i";*/ $("input[name='PROPERTY[24][0]']").attr("pattern", pattern); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="PROPERTY[24][0]" size="25" value="" required="" type="text"> 

None of these methods work.

2 answers 2

In var pattern = "\S+@[az]+.[az]+"; \S is actually an invalid escape sequence, in which the backslash is deleted and only the character after it remains. Those. the pattern is S+@[az]+.[az]+ . To get the \S , you must double the backslashes.

It can be done differently.

To check the entered email address, simply use the built-in type HTML5 type="email" :

 input:valid { color: navy; } input:invalid { color: red; } 
 <form> <input name="PROPERTY[24][0]" size="25" value="" required="" type="email" /> <input type="Submit" /> </form> 

Since the example above accepts values ​​of type one_two@три , you can use your regular expression in pattern :

 input:valid { color: navy; } input:invalid { color: red; } 
 <form> <input name="PROPERTY[24][0]" size="25" pattern="\S+@\S+\.\S+" value="" required="" type="text" /> <input type="Submit" /> </form> 

pattern="\S+@\S+\.\S+" means that the entire line must consist of characters other than spaces, there must be an @ and in the middle . .

Note that the template defined in the pattern attribute must by default find a complete string match, therefore special metacharacters for the beginning ( ^ ) and end ( $ ) of the string are not needed. Also in this case do not need to double back slashes.

  • @Wictor Stribizew, why masks ^ and $ do not work. in my problem (second and third pattern) - NNN
  • Where do not work? I don’t know if there is any in Russian, but look here in English - it’s not any subset (somewhat as if it is ated ^(?: at the start of the pattern and a )$ at the end). . Those. These "masks" are automatically added to the HTML5 pattern attribute template. - Wiktor Stribiżew
  • Need toString , as did koks_rs - NNN
  • In general, the code needs anchors, see jsfiddle.net/h5m8v88c - Wiktor Stribiżew
  • one
    Here you have a point unshielded - var pattern ='\\S+@\\S+\\.\\S+'; - Wiktor Stribiżew

Try this

  var pattern =/\S+@[az]+.[az]+/; 

Link

  • ari@inbox.ru not working - NNN
  • @NNN, well, because the rehex is a curve. I understand the question about the fact that the pattern itself does not work, and not about how to write a validation for soap. - koks_rs
  • var pattern = '\\ S + @ [az] +. [az] +'; NADO - NNN