Abby Lee wrote:
I have a for statement in my validation process
I do a for loop becasue I don't know how many rows of items there
are...I need to validate each item in each row.
If I remove the "document.f orms[0].submit(); it works perfectly...the
code stops at each field that is not formated properly...but never
submits. However if I leave the submit() in there, the page submits
before they have a chance to fix problems.
How can I get both to work properly?
function validate(){
for(i=1;i<=rows ;i++){
if(column 1 is formated right){
alert("message" );
here.focus();
}
if (column 2 is formated right){
alert("message" );
here.focus();
}
document.forms[0].submit();
}
This idea you have that javascript that is supposed to interact with
HTML can be considered in isolation form the HTML it is interacting with
is misguided.
Normal form validation is done by putting an - onsubmit - attribute in
the opening form tag that calls the validation function and returns
whatever value that function returns:-
<form name="x" action="http://example.com/exPage.jsp"
onsubmit="retur n validateThisFor m(this);">
- The example - validateThisFor m - function would then return - false -
if the form did not validate (cancelling the submission of the form and
giving the user a chance to correct it), or it would return - true - and
allow the form to be submitted.
Your function returns neither true or false, and you have not provided
any information on the context in which it is used. But, generally,
directly calling the - submit - method of a form is a bad idea that
results in a client-side scripting dependency that is avoidable with the
normal use of HTML forms and offers no benefits in return for that
needless dependency.
Richard.