PDA

View Full Version : Javascript validation for multiple text fields with identical names



roger_v
08-24-2009, 02:25 PM
Hello All,

I have a form that has several textboxes with identical names. I figured out a way to validate them by going row by row in the DOM. The problem is that I have a checkbox associated with each row and I need to validate only those rows that are checked and ignore the ones that aren't. Any ideas on how this might be accomplished? thanks,

Here is my validating procedure:


function isValid(){
var isDecimal_re = /^\d+(\.\d+)+$/; //to check for decimal input
var texts=document.getElementsByTagName("input");

columns=4;
for (i=0; i<texts.length; i++)
{
if (texts[i].type=="text")
{
if (!isDecimal_re.test(texts[i].value))
{
columnId=(i%columns)+1;
rowId=Math.ceil(i/columns)+1;
switch(columnId)
{
case 1:
alert("Invalid Number, has to be decimal");
texts[i].focus();
return false;

case 2:
alert("Invalid Number, has to be decimal");
texts[i].focus();
return false;

case 3:
alert("Invalid Number, has to be decimal");
texts[i].focus();
return false;

case 4:
alert("Invalid Number, has to be decimal");
texts[i].focus();
return false;
}
}

}

}
}


I have a checkbox named counter associated with each row and I need to validate only those rows where counter is checked. I tried using the getElementById for the checkbox and see if it is checked but I'm having trouble determining where the end of the row is for that particular checkbox! :confused:

prasanthmj
08-24-2009, 03:20 PM
Are the inputs and check boxes dynamically generated? can't you name them like mytest1 mytext2 etc and the checkboxes like chk1 chk2 etc?

roger_v
08-24-2009, 09:27 PM
Hi,

Yes the input boxes and check boxes names are dynamically generated. I have a textbox_#currentrow# that assigns a unique name to each of these as well as the checkboxes. I'm a noob to Javascript and don't know how I can reference them in the document. thanks,

davelf
08-25-2009, 06:02 AM
i don't know the simple code for this validating, but i find a plug-in that you may be need:

try open this one
http://www.webresourcesdepot.com/inline-form-validation-with-jquery/

you can use the same name for your textbox, with different alert. But you need to learn the javscript, how it's work.
good luck:)

roger_v
08-25-2009, 01:43 PM
Thanks davelf,

Although I'm even more of a noob with jquery than js, I'll give it a shot and see if I can figure a way out to customize it for my app. without spending too much time on it. thanks again,

roger