Click to See Complete Forum and Search --> : Dynamic Array
nbenton99
12-29-2003, 12:57 PM
Hi I'm working on a complex form validation for a dynamic form. Since the form itself if dynamic and can change in various ways having different types of fields and number of fields. All I'm trying to do right now is set up a flag check on whether or not a field is correct or not.
Basically I want to dynamically create an array of 1's for the number of fields that there are. When I run a validation on a field if it is incorrect I want to turn that fields 1 to 0, and once it is fixed turn it back to 1. so that I can then do a check to see if there is a field that is still incorrect.
I know this sounds like a complicated way to do a validation but for numberous reason due to the depth of the dynamic forms I can't do an onsubmit form validation there are too many variables each form field is being check onBlur and I need to force the user to fixed the mistake if I set the focus back to the field I get into an endless loop.
Anyways basically my question is if anyone has experience in building dynamic arrays in Javascript. How do I build one.
TIA
Nikki
stoodder
12-29-2003, 01:35 PM
hmm okay so give this bit a try:
var formname = "The Forms Name";
var fields = Array(put all field names here);
var validate = Array();
function check_fields() {
if(document.form.fields[0].value == " Value To Check" && document.form.fields[0].value.length >= MinLength && document.form.fields[0].value.length <= MaxLength) {
validate[0] = 1;
} else {
validate[0] = 0;
}
also remmeber that you can make more then one dimension in arrays, for example:
var guy = Array(Array(Array(stuuf),Array(more stuffe)),Array(Event More stuff),Array(Wow thats alot of stuff)));
this would create an arraythat oculd look like this:
guy[1][1][2]
hmm i dont know if that helps at all, remmeber to add more if statements to check for the other fields,, but givew that a try, if you need any further assistance give me an email at rickyallen13@yahoo.com oh and make sure you make the title of the email osmehting like Javascript Help or whatever otherwise ill throw it away.
furious70
12-29-2003, 01:45 PM
I'd first like to suggest not validating like this. Doing onblur for every form element can become very annoying to your user. They may wish to fill the form out in their own order, or may even just accidentally tab off for instance. THey may not have wanted to have their response considered 'ready for submission' yet, since they expect that to happen when they click the submit button. Why do you believe an onSubmit will not work? I'm sure I'm not the only one that has forms with dozens of inputs that need to be validated, and I do it with onSubmit, putting *'s by all offending fields and giving useful text messages as to what's wrong with each field.
stoodder
12-29-2003, 01:46 PM
exactly what i was thining also, what i would suggest is you take up a little bit of beggingin PHP its fairly easy languag ethat runs server side and is good for validating forms after they are submitted.
furious70
12-29-2003, 01:52 PM
well, IMO I'd still do it client side with JS, just not in the onblur on each element manner
nbenton99
12-29-2003, 02:49 PM
Thanks guys,
Those are all good suggestions which I've suggested as well. Unfortunately this is a project that I have inherited only a fraction of. This is only one piece to a huge puzzle. I actually am quite familiar with PHP infact it is my language of choice but this application is ASP and Javascript based. And my major struggle is trying to get something to work when I'm not seeing all the pieces. So I'm trying to do my part in a separate JS file that will work with the dynamic form built in ASP. I did not build this page and It is really deep. The Reason that I you can't do the onsubmit (maybe I'm wrong but I don't see how to do it that way.) is that every form field is dynamic how many fields, the name of fields, what type of fields (Drop boxes, radio boxes, text, etc. ) and what type of information is in each is constantly changing and you need to know all of these variables in order to know what type of validation you need. so I've set the onblur funtion to be dynamic as well based on the type of field it is. If it is a date field onblur run a date validation. I can see how to do that on a submit. Anyways thank for the suggestions and for the sample of the array I'll try it. And I'm open to any other suggestions.
TIA
Nikki
furious70
12-29-2003, 04:29 PM
couldn't you just get all elements under the form (or get all children nodes under the form) and then loop through them, check their type, and process them?
nbenton99
12-30-2003, 10:01 AM
I'm not sure how you mean.
furious70
12-30-2003, 10:26 AM
why not use the document.form.elements[] array? It will contain all the elements in the form. I haven't tried it, but something like
aEles = document.form.elements;
for (i=0; i < aEles.length; i++)
{
//special field to be handled
if (aEles[i].name == "input1")
{
//do special case validation here
}
else if (aEles[i].type == "input")
{
//do input box validation here
}
else if (aEles[i].type == "radio")
{
//do radio check here
}
something along those lines. If you know the name of certain elements that need to be handled extra special, or you have radio groups or something, you'd need to check for their names and handle them special eariler in the code, else drop into the generic type validation like I've done above.
nbenton99
12-30-2003, 10:31 AM
That might just work I'll try that thanks.
Nikki