Simple form validation


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.