Posted By

gembry on 12/19/08


Tagged

form input validation


Versions (?)

Basic Signup Form Validation


 / Published in: JavaScript
 

I came across this little snippet... and thought it was rather nice.

  1. <SCRIPT LANGUAGE="JavaScript">
  2. function validate(form) {var e = form.elements, m = '';
  3. if(!e['firstname'].value) {m += '- First name is required.\n';}
  4. if(!e['lastname'].value) {m += '- Last name is required.\n';}
  5. if(!e['address'].value) {m += '- Address is required.\n';}
  6. if(!e['city'].value) {m += '- City is required.\n';}
  7. if(!e['postcode'].value) {m += '- Postcode is required.\n';}
  8. if(!e['telephone'].value) {m += '- Telephone number is required.\n';}
  9. if(!/.+@[^.]+(\.[^.]+)+/.test(e['email'].value)) {
  10. m += '- E-mail requires a valid e-mail address.\n';
  11. }
  12. if(!e['username'].value) {m += '- Username is required.\n';}
  13. if(!e['password'].value) {m += '- Password is required.\n';}
  14. if(e['password'].value != e['confirm'].value) {
  15. m += '- Your password and confirmation password do not match.\n';
  16. }
  17. if(m) {
  18. alert('The following error(s) occurred:\n\n' + m);
  19. return false;
  20. }
  21. return true;
  22. }
  23. </SCRIPT>

Report this snippet  

You need to login to post a comment.