Xavier Cabezas

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromXavier Cabezas
ToMe
SubjectPassing the value returned after an onblur event.
Date6 December 2006 18:56
I have search for more than a week an answer to the following.  I am very
impressed with your knowledge and I hope you will be able to help me.

I have many JS functions that I use as part of an universal form validator.

For Instance.

function TextBoxValidation(object, isRequired, intMinLen, forceNumeric,
forceEmail){
var blFoundErrors = false;
if (isRequired && object.value == ""){

alert('This field is required.  Please enter a value for this field.');
blFoundErrors = true;
object.focus();
object.select();

}
else if ((GetFieldLength(object) < intMinLen) && (intMinLen > 0)){

alert('Please enter a value that is at least ' + intMinLen + ' characters
long.');
blFoundErrors = true;
object.focus();
object.select();

}
else if ((isNaN(object.value)) && (forceNumeric)){

alert('Please enter a numeric value.  Do not use $ or commas');
blFoundErrors = true;
object.focus();
object.select();

}
else if ((!isEmail(object)) && (forceEmail)){

alert('Please enter a valid email address.');
blFoundErrors = true;
object.focus();
object.select();

}
return blFoundErrors;

}// end TextBoxValidation function

Then I call the validation as follow.

<input type="text" name="forum_name" id="forum_name" value="General Forum"
size=45
maxlength=255 onblur="TextBoxValidation(this,true,0,false,false);"
class=frmInput>

This works great if the user does the focus() and then blur() on each form
field but of course that's not always the case.

I am trying to call a function when the form is submitted that will loop
through every field in the form and check for errors.

function CheckForErrors(strFormName){

var blFoundErrors = false;
var objFrm = document.forms[strFormName];
var objFld;
for (i = 0; i < objFrm.elements.length; i++) {

objFld = objFrm.elements[i];
objFld.disabled = false;
switch (objFld.type) {
case 'text':

//HERE IS MY PROBLEM
//I cannot find a way to call the TextBoxValidation function and pass the
value returned to my var blFoundErrors

break;
//other cases go here
}// end switch

}// end for loop
return blFoundErrors;

}//end CheckForErrors function

I tried calling objFld.focus() and objFld.blur but I still cannot pass the
value returned from TextBoxValidation

I would greatly appreciate your help on this.

Thank you so much in advance.

Xavier Cabezas
FromMe
ToXavier Cabezas
SubjectRe: Passing the value returned after an onblur event.
Date8 December 2006 11:57
Xavier,

> I tried calling objFld.focus() and objFld.blur but I still cannot pass the
> value returned from TextBoxValidation

The trouble is that your onblur event handlers do not return any value, so
when you call them manually, you get nothing in return. It would be easy to
change this:
onblur="TextBoxValidation(this,true,0,false,false);"
to this:
onblur="return TextBoxValidation(this,true,0,false,false);"
But that would have the effect that blurring would be canceled when they do
not make any mistakes - not good.

So I am assuming that CheckForErrors will call TextBoxValidation directly,
and not use the foo.onblur() methods. You need some way for CheckForErrors
to know what all of those true/false values are. You can duplicate it all in
the CheckForErrors function, but I recommend you store all those true/false
values in a global variable, and get the event handlers, and CheckForErrors
to use those values, to save duplication. A simple object with properties
matching the names of the inputs would be easiest:
var vval = {
  forum_name: [true,0,false,false],
  forum_email: [false,5,false,true],
  forum_something: [false,0,true,false] //no comma on the last one!
};

The blur event handler then becomes:
onblur="TextBoxValidation(this,vval[this.name][0],vval[this.name][1],
  vval[this.name][2],vval[this.name][3]);"

Now, onto the problem;

Since TextBoxValidation already returns a value, all you have to do now is
inside CheckForErrors, assign that return value to a variable, and use it.

var tmv = vval[objFld.name];
if( TextBoxValidation(objFld,tmv[0],tmv[1],tmv[2],tmv[3]) ) {
  blFoundErrors = true;
}


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromXavier Cabezas
ToMe
SubjectRe: Passing the value returned after an onblur event.
Date8 December 2006 16:06
Mark,

I really appreciate you going over my code and I thank you so much for your
advise.

I am going to follow your recommendations.

Thanks again,

Xavier
FromXavier Cabezas
ToMe
SubjectRe: Passing the value returned after an onblur event.
Date14 December 2006 16:25
Mark,

Just want to thank you again for your help and advice.  The
multi-dimensional array solution works great!

Thanks again!

Xavier
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.