Is this efficient?

I’m planning out some JS and I’m curious as to what you guys think of this approach. I’m validating a form and I have about 10 - 15 required fields.

The approach I’m thinking of taking is to create two global arrays corresponding to the number of required fields(one of 1’s and the other 0’s). Then I call the function onChange and pass ID for the question into the function.

Since all form fields will not be verified in the same manner I use the supplied ID to determine which portion of a switch statement I need to use. I know that some fields will use the same so I’m going to use a local variable to save the type of validation I’m going to use.

On the appropriate branch of the switch I use getElementsById (using the ID passed into the function) to local the question and test the field. If input value is valid then I make the error message invisible (if it’s visible) then make that question’s deisgnated value in the array a 1 (location based on supplied ID).

Once the switch completes, I compare the two global variables and if the initially 0 array is now all 1’s the submit button becomes enabled.

Is this the most efficient way to do this? Or am I just doing it wrong? I’m trying to make it execute as little code as possible after each field is changed.

I’m still working out my psuedo on paper and would appreciate any input on this matter. :slight_smile:

Thanks in advance guys, this site has proved itself to me to be an immensely useful tool.

i suppose your writing your own code rather than using a library such as prototype, to make things easier?

Anyway if i understand you correctly you want to do some client side validation on a big form with several types of validations correct?

Why not build an object with the several regular expressions needed for the several validations, and in the form use something like class=“validate mail”.

Then create your validate function where you pass the element id and type of validation required, where you fetch the matching regexp from the object.

Then create a function to see if the form is present in the page and attach the onchange event to the fields that have the “validate” class attribute.

Take notice that IE has some quirks with form field events.

I suppose there could be a number of different approaches to this.

i suppose your writing your own code rather than using a library such as prototype, to make things easier?

I’m hoping on writing my own code mainly so that I fully understand what’s going on. I’m also using this as a learning experience.

Anyway if i understand you correctly you want to do some client side validation on a big form with several types of validations correct?

I was hoping on using a combination of client and server. For the client I just wanted to be sure that the correct fields were entered and that they don’t contain any unwated characters. Then using server side validation to make sure that the username and email address were not used to prevent duplicates in the database.

Take notice that IE has some quirks with form field events.

Believe me… I know… my first forray into JS was very rocky due to this. Quite thankfully, I can’t wait until we’ll only have to worry about IE9. They seem to have finally got on board with it and they completely rewrote the JS engine. Too bad we’ll still have to worry about the earlier versions for the next ten years.

I suppose there could be a number of different approaches to this.

-nod- Mainly, I know that a quite majority of people who will be using this will be on ie6/7 so I’m attempting to execute as little code as possible to accomplish my goal. I’m using the global arrays so that validation only needs to run on a specific field and then updates global and determines if the form is correct by just checking two arrays.