The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

AjaxNewbie (1) [Avatar] Offline
#1
My questions may seem too trivial since I am very new to JavaScript/DHTML/AJAX but have been programming a while in JAVA.

We are trying to incorporate the AJAX form validation into our code. We have plenty of HTML form that can benefit from this approach. Where the user is not allowed to submit the form untill all the errors are gone.

A popular form field that appears across all our forms are Address Validation specially City/State/Zip.

Since the form field validation in all our forms has the same kind of pattern going on -
a) Validate on the fly,
b) The empty div next to the form field gets populates with red or green error message based on bad or good result respectively
c) Disable the form submit button in case of error

So if we write a javascript file say ajaxvalidation.js that encapsulates the AJAX interaction given the emptyDivToWrite, ButtonToDisable, FormFieldToCheck as parameters to the validateField() method in the javascript we can reuse the same script file across all form fields and we do not have to cut-paste everytime to each form we come up with.

Can you please let us know if the approach above works fine if we want to reuse the code instead of rewriting the ajax form validation javascript in each HTML form page?


Thanks in advance,
AjaxNewbie

Message was edited by:
AjaxNewbie

Message was edited by:
AjaxNewbie
Pascarello (208) [Avatar] Offline
#2
Re: Ajax Form Validation
It is better to generate a method that is reusable than hardcoding values.

If you have multiple validations on the same page that use this code, make sure you make it OO so you can have it fire off multiple requests without interering with the others.

Eric