Reputation: 4956
Problem: I am having a textarea in which I want to restrict users from entering their email id. So,that when they type email id the text area should turn red and pop-up a message saying "you can't enter your email address in this field".
My solution: Here is the simple HTML code for textarea:
<textarea class="replyToClient" placeholder="Message"></textarea>
I am currently using the below mentioned javascript code to detect email id and replace it with NULL.
<script type="text/javascript">
//EMAIL ID DETECTION AND REPLACEMNET
$(function() {
$(".replyToClient").change(function() {
$(this).val( function(idx, val) {
return val.replace(/\b(\w)+\@(\w)+\.(\w)+\b/g, "");
});
});
});
</script>
What this does is replaces something like "[email protected]" written in textarea with blank.
Expectation: I am trying to build a strong regexp here so that I can detect email id in any form such as "example@gmail" or "example at the rate gmail dot com" and then (the easy part)replace it from the textarea or come up with a pop-up.
So, my concern is mainly on the regular expression to detect email id in it's original format and also in the possible broken formats as mentioned above.
Upvotes: 0
Views: 144
Reputation: 1230
A bit of update to Roberts answer. The regex shown does not process dashes, underscores and dots are not taken accout for, which are standard for e-mails, as well as england based e-mails (.co.uk)
This is important for the first word. [email protected]
and john-wick at gmail dot com
will respectively result in [email protected]
and wick at gmail dot com
, leaving unwanted words in your text area. Try:
[A-Za-z0-9.\-\_]*?@\w+(\.\w+)?(\.\w+)?|[A-Za-z0-9.\-\_]*? at \w+ dot \w+( dot \w+)?
The regex seems to work for all the mentioned possibilities as [email protected]
, john-wick at gmail dot com
and [email protected]
, john-wick at example dot co dot uk
.
If there are other broken formats people tend to write their e-mails, they should probably be dealt with one at a time.
Upvotes: 1
Reputation: 115
give this a shot:
val.replace(/\w+@\w+(\.\w+)?|\w+ at \w+ dot \w+/ig, "");
Cheers
Upvotes: 1