I am very new to coding and even newer to Javascript. My brain works by thinking in general/abstract all the time and I am lazy, so I hate doing anything remotely repetitious. In that vein, I wrote this script to chuck through a form, any form and check for nulls where there shouldn't be. I used alerts to notify, but you could just as easily add a red error tag and focus() back. All you have to do is add informative and readable titles to fields you want null-validated. And add this to your onsubmit="return validation();" line. All hidden or button or submit, etc. inputs are ignored if you don't add a title to them.
Expand|Select|Wrap|Line Numbers
- function nullValidation() {
- var x = document.getElementById("myForm");
- for (var i = 0; i < x.length; i++) {
- if(x.elements[i].value == "" && x.elements[i].title) {
- alert("Please enter or select " + x.elements[i].title + ".");
- x.elements[i].focus();
- return false;
- }
- }
- return true;
- }
regards,
oh4real