0

In the email input field I am checking for illegal characters using regex, like this:

$("#reg_email").keyup(function(event) 
{        
    var regex = /[()-/+{|}\\[\]?!*+^$<>&#~"'%=_:;]/g;

    if ($("#reg_email").val().match(regex)) 
    {
        $("#reg_email").notify("Illegal characters: \n( ) - / \\ + { } | [\ ] ? ! * + ^ $ < > & # ~ % = _ : ; '",{position:"right", className:"warn", autoHideDelay: autoHideDelay});
        this.value = this.value.replace(regex, "");
    }
});

Here is the markup:

<input id="reg_email" type="email" class="form-control placeholder" placeholder="" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>

But I've noticed that now I cannot enter a full stop character in the input field, and that is not what I want, since email addresses will contain a full stop. I am aware that '>' has the same keycode as a full stop, but currently I am struggling with finding another way.

How can I achieve this alternatively? Thanks

Maistrenko Vitalii
  • 994
  • 1
  • 8
  • 16
RollerMobster
  • 864
  • 1
  • 10
  • 28

1 Answers1

2

You have )-/ in your set, which creates a range that matches )*+,-./ (according to the ASCII table) and not just the characters )-/ literally. Escape the - or put it at the start or end of the character class as the examples below show:

[()\-/+{|}\\[\]?!*+^$<>&#~"'%=_:;]    # escaped \-
[-()/+{|}\\[\]?!*+^$<>&#~"'%=_:;]     # hyphen at start of class
[()/+{|}\\[\]?!*+^$<>&#~"'%=_:;-]     # hyphen at end of class
ctwheels
  • 21,901
  • 9
  • 42
  • 77