Finding email and phone number with JavaScript

A while back we did an article about email and phone number cloaking in WordPress. What we used is the inbuilt editor of WordPress and use hooks and filters that work with that editor.

We used the filter ‘wp_insert_post_data’ and we used php code. We tested against ‘post_title’ and ‘post_content’. Recently we needed to cloak email and phone numbers that was typed in a textarea input. We achieve this by using plain simple JavaScript.

It is really easy and very little code to get the job done.

See this snippet:

<script type="text/javascript">
    function maskemail(){                                
        //first we need to get the the text, we do this by using getElementById to get the textarea data
        var gettext = document.getElementById("thetextarea").value;

        //now we check if there is any email in the textarea.
        // I use /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi to get the email
        if (gettext.match(/([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+.[a-zA-Z0-9._-]+)/gi)){
            
            //Now alert if there is an email in the textarea
            alert("Found an email");

            //Now we search to see if there is any phone number in the textarea
            //I use /[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}/gi to get the phone format.
        }else if (gettext.match(/[+]?[(]?[0-9]{3}[)]?[-s.]?[0-9]{3}[-s.]?[0-9]{4,6}/gi)){
            alert("Found a phone number");
    }
</script>

In the next tutorial, I will show how to use this code and mask the email and phone number.

wordpress
Once a week we send a digest with the most popular articles.

We are more than just a Hosting company!

AdminWP Host, Maintain (Site-care), Secure and Backup WordPress websites.