Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unknown size list to be validated

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
Hey guys, I have a list being generated from a previous page, with open text boxes for information needed to be input. How would I validate the text boxes when the "submit" button is pushed?

Thanks
Mike
 
Code:
function validate(f) {
	var els = f.elements;
	var flag = true;
	
	for (var x = 0; x < els.length; x++) {
		if (els[x].type && 
			els[x].type.toLowerCase() == &quot;text&quot;) {
			if (!els[x].value) {
				alert(&quot;Please enter a value.&quot;);
				els[x].focus();
				flag = false;
			}
		}
	}
	return flag;
}

...

<form onsubmit=&quot;return validate(this);&quot;>

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Thanks, that works great...now I have a question. What if I only wanted certain things to be checked, you know, like needed stuff and let other optional boxes be blank and not get checked? Would I need to do something like

els = f.table_name.elements

or something along those lines? I have multiple things on the form, but only a certain section needs to be validated.

Thanks again
Mike
 
it depends...the items that you want validated: are they the same items every time? do they vary in number? are they named?



=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
the items I need checked are text boxes, all given names by the server side ASP function depending on the number of items...&quot;text1&quot;, &quot;text2&quot;...&quot;textN&quot;....generated within the code. I have extra boxes, such as a comment box, that don't need to be checked, but are still on the form. They will vary in number from form generation to form generation, depending on the query results...
 
if these text boxes are the only fields that need to be validated, and they're always named &quot;textN&quot;, then this should work:
Code:
function validate(f) {
    var els = f.elements;
    var flag = true;
    
    for (var x = 0; x < els.length; x++) {
        if (els[x].name && 
            /^text\d+$/.test(els[x].name)) {
            if (!els[x].value) {
                alert(&quot;Please enter a value.&quot;);
                els[x].focus();
                flag = false;
                break;
            }
        }
    }
    return flag;
}


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
that works like a charm, but I changed it a little to check this:
if (!document.f[&quot;value_txt&quot;+i].value)

to see if the box had been filled. Other than a little more additions, it works great.

Thanks
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top