Posted By

noah on 03/17/07


Tagged

email forms validation validator validate


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jonhenshaw
vali29
leitmotiv


validate email address when submitting a form


 / Published in: JavaScript
 

based on the 'valid email' snippet by krisdb

  1. function validateEmail ( fieldId ) {
  2. var field = document.getElementById( fieldId );
  3. var address = field.value;
  4. //based on 'valid email' by krisdb via snipplr.com
  5. var filter=/^.+@.+\..{2,3}$/;
  6. return (function (){
  7. if (filter.test(address))
  8. return true;
  9. else {
  10. if (address == '') {
  11. alert('Please enter a valid email address before submitting the form.');
  12. } else {
  13. alert('"' + address + '" is not a valid email address. Please correct your address and try again.');
  14. }
  15. field.focus();
  16. return false;
  17. }
  18. })();
  19. }
  20. window.onload = function() {
  21. clearOnInitialFocus('email');
  22. document.forms[0].onsubmit = function () { return validateEmail('email') };
  23. }

Report this snippet  

You need to login to post a comment.