We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

fuzzylogic on 04/09/08


Tagged


Versions (?)


Who likes this?

4 people have marked this snippet as a favorite

SmpleJohn
osdesk
blackabee
korzhik


Simple form validation


Published in: JavaScript 


  1. function Validate(CheckFields) {
  2.  
  3. var ShowAlert = false;
  4. var ArrFields = CheckFields.split(",");
  5. for (var x = 0; x < ArrFields.length; x++) {
  6. var TargetField = document.getElementById(ArrFields[x]);
  7.  
  8. if (TargetField.value == '') {
  9. TargetField.style.backgroundColor = '#EF9999';
  10. TargetField.onclick = function(){
  11. this.style.backgroundColor = '#FFF';
  12. }
  13. ShowAlert = true;
  14. }
  15. }
  16.  
  17. if (ShowAlert == true) {
  18. alert("You have not filled in all the required fields. \n Please fill in all the fields marked in red.");
  19. scroll(0,0);
  20. } else {
  21. document.form1.submit();
  22. }
  23.  
  24. }

Report this snippet 

You need to login to post a comment.