Home > Uncategorized > Javascript: prevent entering of Email addresses

Javascript: prevent entering of Email addresses

I just designed a nice piece of Javascript that I thought I’d like to share. Its some code that can be used to prevent people typing email addresses into textboxes – where for instance, you want to control the flow of communication between client and end-user.
 
<BODY onkeypress="removeEmailsFromAll(‘textarea’)">
Then the Javascript
 
  function removeEmailsFromAll(type)
  {
  elements = document.getElementsByTagName(type);
  for (var i = 0; i < elements.length; i++) {
     removeEmails(elements[i]);
  }
   }
  function removeEmails(element)
  {
    var EmailRegex = "([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})";
 var re = new RegExp(EmailRegex, "g");
 try
 {
     element.value =  element.value.replace(re,"");
 }
 catch(ex)
 {
     element.innerHTML =  element.innerHTML.replace(re,"");
 }
  }
 
 
 
Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: