javascript - Regular expression for email id to avoid email input in a textfield -
problem: having textarea in want restrict users entering email id. so,that when type email id text area should turn red , pop-up message saying "you can't enter email address in field".
my solution: here simple html code textarea:
<textarea class="replytoclient" placeholder="message"></textarea> i using below mentioned javascript code detect email id , replace null.
<script type="text/javascript"> //email id detection , replacemnet $(function() { $(".replytoclient").change(function() { $(this).val( function(idx, val) { return val.replace(/\b(\w)+\@(\w)+\.(\w)+\b/g, ""); }); }); }); </script> what replaces "example@gmail.com" written in textarea blank.
expectation: trying build strong regexp here can detect email id in form such "example@gmail" or "example @ rate gmail dot com" , (the easy part)replace textarea or come pop-up.
so, concern on regular expression detect email id in it's original format , in possible broken formats mentioned above.
a bit of update roberts answer. regex shown not process dashes, underscores , dots not taken accout for, standard e-mails, england based e-mails (.co.uk)
this important first word. john-wick@gmail.com , john-wick @ gmail dot com respectively result in wick@gmail.com , wick @ gmail dot com, leaving unwanted words in text area. try:
[a-za-z0-9.\-\_]*?@\w+(\.\w+)?(\.\w+)?|[a-za-z0-9.\-\_]*? @ \w+ dot \w+( dot \w+)? the regex seems work mentioned possibilities john-wick@gmail.com, john-wick @ gmail dot com , john-wick@example.co.uk, john-wick @ example dot co dot uk.
if there other broken formats people tend write e-mails, should dealt 1 @ time.
Comments
Post a Comment