470,593 Members | 2,437 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,593 developers. It's quick & easy.

submiting after for loop in page validations

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.forms[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();
}
Jul 23 '05 #1
6 1263
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.forms[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 will always submit the form, include a "return false" after an
error, or better yet rewrite the function so that it returns a boolean
value.

<form onsubmit="return validate()">
Mick
Jul 23 '05 #2
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.forms[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="return validateThisForm(this);">

- The example - validateThisForm - 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.
Jul 23 '05 #3
Mick White <mw******@BOGUSrochester.rr.com> writes:
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.forms[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 will always submit the form, include a "return false" after an
error, or better yet rewrite the function so that it returns a boolean
value.

<form onsubmit="return validate()">
Mick

My code just submits without doing any validation. Understand I'm new to
this..can you be of more help?

function validate(){
for(i=1;i<=rows;i++){
if(column 1 is formated right){
alert("message");
here.focus();
return false;
}
if (column 2 is formated right){
alert("message");
here.focus();
return false;
}
}

--------<sbip>---------
<input type="submit" name="Submit" value="Submit Changes" onSubmit="return
validate()">
Jul 23 '05 #4
Abby Lee wrote:
My code just submits without doing any validation. Understand I'm new to
this..can you be of more help?

function validate(){
for(i=1;i<=rows;i++){
if(column 1 is formated right){
alert("message");
here.focus();
return false;
}
if (column 2 is formated right){
alert("message");
here.focus();
return false;
}
}


I am not sure if you're trying to validate form elements, it seems not.
First, I would name the function something meaningful.
function validateColumnFormat(){
for(i=1;i<=rows;i++){
if(column 1 is formatted wrong){
alert("message");
here.focus();
return false;
}
if (column 2 is formatted wrong){
alert("message");
here.focus();
return false;
}
}
return true;
}

Then you may test the condition:

if(validateColumnFormat()){ //do stuff }

Or set a boolean , dependant on the condition:
var ready=validateColumnFormat();
Or a variable
var foobar=validateColumnFormat()?"white":"grey";

Mick

Jul 23 '05 #5
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.
[...]
function validate(){
for(i=1;i<=rows;i++){


Non sequitur. A "for" loop is determined and as such the exact opposite
of what you want to do if you do not know the number of rows, unless your
evil[tm] global "rows" determines the number of rows previously (if so,
then why don't you determine it within the method?).
PointedEars
Jul 23 '05 #6
You can just use an ordinary submit button -- take that
submit() line out of the code. Then, add the following
to the <form> declaration:

onsubmit="return validateForm();"

Then, have your function keep a boolean (true/false),
declared true at the top, and make it false if the
validation fails at any point. Return that value
at the end of the function.

Example:

function validateAddPage(){

var allOkay = true;
var errMsg = '';

if (document.forms['frmAdd'].txtTitle.value == ''){
errMsg = errMsg + 'A project title must be entered.\n';
var allOkay = false;
}

if (document.forms['frmAdd'].drpRelevance.selectedIndex == 0){
errMsg = errMsg + 'A strategic relevance must be entered.\n';
var allOkay = false;
}

if (document.forms['frmAdd'].drpCompany.selectedIndex == 0){
errMsg = errMsg + 'A company must be entered.\n';
var allOkay = false;
}

if (document.forms['frmAdd'].txtCompletion.value == ''){
errMsg = errMsg + 'A requested completion date must be entered.\n';
var allOkay = false;
}

if (!allOkay){alert(errMsg);}

return allOkay;

}
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.