PDA

View Full Version : javascript forms validation


flann
12-16-2006, 10:12 PM
i'm trying to pass a variable into a function and use it as follows.

document.form1.<< this is where i want the variable >>.value

i'm trying to do it this way so i can use the same function for validating multiple form elements that require the same validation. Is this possible?

Jon Hanlon
12-18-2006, 04:29 PM
function val(obj) {
if (!obj) return false; // obj don't exists
var val = obj.value;
if (val == "frodo" || val == "bilbo") {
alert("Sorry, no Hobbits")
return false;
}
//... put other tests here
return true;
}

Be sure to call the function with an object reference, NOT a string.
<input type="text" onchange="return val(this)">
<input name="blurb" onchange="val(document.form1.blurb)">

flann
12-20-2006, 11:35 AM
thank you, that works great.