Search code examples
phpjavascriptformsemailspam-prevention

How to prevent browser from replacing email in text field with JavaScript?


I have a field in a form like so:

<input type="text" name="email" />

I'm using PHP to check the validity of the form, which contains other fields in addition to this one. If the validity check fails, I store the values in the session and output the form again with an error message and the form values prepopulated.

If I add an email, say "person@example.com", and the form invalidates, then "person@example.com" gets replaced with JS:

 <script type='text/javascript'> <!-- var prefix = 'ma' + 'il' + 'to'; var path = 'hr' + 'ef' + '='; var addy34269 = 'person' + '@'; addy34269 = addy34269 + 'example' + '.' + 'com'; document.write('<a ' + path + '\'' + prefix + ':' + addy34269 + '\'>'); document.write(addy34269); document.write('<\/a>'); //-->\n </script><script type='text/javascript'> <!-- document.write('<span style=\'display: none;\'>'); //--> </script>This email address is being protected from spambots. You need JavaScript enabled to view it. <script type='text/javascript'> <!-- document.write('</'); document.write('span>'); //--> </script>

Needless to say this is not what I want. This happens in Chrome and Firefox; I'm not sure about the others. How can I prevent the browser from replacing the email after form invalidation?

I had thought of using str_replace to make the email not look like an email, then use JS to do another string replacement to bring it back to its original value, but I'd like to do this without JS if possible. Any ideas?


Solution

  • As you (or your host) is using Joomla! you can disable the "Email cloaking" Plugin completly or disable the cloaking feature for a single email as discussed here