Showing posts with label Loop Form Elements. Show all posts
Showing posts with label Loop Form Elements. Show all posts

Wednesday, May 30, 2007

How to loop through HTML Forms collection and validate control values

I have often wondered how to validate form controls that were dynamically generated from a server-side script. Here's my solution in JavaScript:

for (j=0; j<document.theform.elements.length; j++)
if ( document.theForm.elements[j].type == "text" )
{
//Perform validation when the control's type is text
}

if ( document.theForm.elements[j].type == "checkbox" )
{
//Perform validation when the control's type is checkbox
}

if ( isNaN (document.theForm.elements[j].value) )
{
//Perform validation if the control's value property does not evaluate to a number
}

if ( Number (document.theForm.elements[j].value) < 0 )
{
//Perform validation if the control's value property evaluates to a number less than 0
}
}

Refer your friends to this blog:
http://programmingcentral.blogspot.com/