...

View Full Version : Basic Form Validation Function



]|V|[agnus
06-24-2004, 05:17 PM
I had an idea for a basic validation function that would take an arbitrary set of id's as arguments, loop over them, and then alert the user to fill in any empty ones and return false to stop submission of the form.



function genericValidation(formID) {
var form = document.getElementById(formID);
for (i=1; i<arguments.length; i++) {
var field = document.getElementById(arguments[i]);
if (form.field.value == "") {
alert("Please fill in all required fields.");
return false;
}
}
}


The for loop starts at 1 because the idea is that the first argument would always be the <form> id. The problem I am having is that the condition that would prompt the alert() function is not being met. Alerting arguments[i] tells me that it is undefined. This is how I am calling the function:



<input type="submit" name="submit" id="submit" value="Add Item" class="button" onclick="return validateNewItem('addItem','item')" />


validateNewItem is just a more specific function that calls genericValidation(), but also includes a condition to check a select box for a valid selection.



function validateNewItem(formID) {
genericValidation(formID);
var parent = document.getElementById("parent");
if (parent.options[parent.selectedIndex].value == "") {
alert("Please select a valid parent category.");
return false;
}
}


That function is working fine, and I know it is calling genericValidation, but again, my problem is with the arguments and handling them or something. Clues?

Thanks.

Willy Duitt
06-24-2004, 05:21 PM
Start counting at 0.....

]|V|[agnus
06-24-2004, 05:25 PM
Please don't comment as if the reason should be obvious. Why should I do that? Is it required of the syntax? If so, great, fine, but I obviously don't know that. I thought starting at 1 would make sense, as I explained already, because I want to ignore the first element of the arguments array.

Furthermore, that didn't seem to make any difference except for somehow breaking the part of the more specific function that actually was working. The form just submitted away, no alerts or anything.

Roy Sinclair
06-24-2004, 05:31 PM
You may be passing multiple arguments to the validateNewItem function but the only call it has to the genericValidate function passes exactly one parameter to the function so you never get the rest of the arguments to the next function.

adios
06-24-2004, 05:33 PM
You're mixing hierarchial (object.property.property) references with element-accessing methods. This:


document.getElementById(arguments[i]);

...should get the field object by itself. So this:


if (form.field.value == "") {

...makes no sense - as you're 'attaching' the already obtained form element reference to a Form object reference. Don't overdo it! The Form reference is unnecessary.

One other thing...don't use 'item', 'parent' as variable names - until you really know what you're doing. Strictly speaking, these aren't reserved words, but they are DOM properties, and in the wrong context, could cause problems. Lots of other choices. :)

Willy Duitt
06-24-2004, 05:36 PM
|V|[agnus']Please don't comment......

Ok, no problem....
I won't [open ignore].....

Have a Nice Day.....

]|V|[agnus
06-24-2004, 05:38 PM
You may be passing multiple arguments to the validateNewItem function but the only call it has to the genericValidate function passes exactly one parameter to the function so you never get the rest of the arguments to the next function.

So, should the call to genericValidation() within' the more specific function specify no arguments then? The generic function is okay though, right? It is my understanding that I can specify certain arguments that will always be present, but that it can receive more arbitrary arguments.

Adios: Duh! Of course! :)

Thanks all...

]|V|[agnus
06-24-2004, 05:39 PM
Ok, no problem....
I won't [open ignore].....

Have a Nice Day.....

Pfft. You know that's not what I'm saying. Your choice, chief.

Roy Sinclair
06-24-2004, 07:49 PM
|V|[agnus']So, should the call to genericValidation() within' the more specific function specify no arguments then? The generic function is okay though, right? It is my understanding that I can specify certain arguments that will always be present, but that it can receive more arbitrary arguments.


If you want all of the arguments to be passed to the genericValidation function then you've got to pass ALL of them from the validateNewItem function, right now you're only passing one of them.

]|V|[agnus
06-24-2004, 08:01 PM
If you want all of the arguments to be passed to the genericValidation function then you've got to pass ALL of them from the validateNewItem function, right now you're only passing one of them.

Right. What I'm saying is that I don't quite understand how to do that then. :) I'll strip down the code to illustrate what I tried:



function validateNewItem(formID) {
genericValidation();
...statements...
}

OR

function validateNewItem(formID) {
genericValidation(arguments);
...statements...
}


They didn't work, and I guess I don't know how to pass along that dynamic list of arguments then. I looked around for help with the solution and couldn't find any answers. I can keep looking, but if you can help that'd be cool.

Roy Sinclair
06-24-2004, 08:23 PM
This simple page demonstrates how to pass the arguments:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Testing</title>
</head>
<body>
<script language="javascript" type="text/javascript">
function f1(args)
{
for (var i = 0; i < args.length ; i++)
{
alert('args(' + i + ')=' + args[i])
}
}
function fa()
{
f1(arguments)
}
fa('Test','This','Function');
fa('Second','Test','Of','This','Function')
</script>
</body>
</html>


'arguments' is a local array so it can be passed on to a called function and handled in the called function as an array as well.

]|V|[agnus
06-24-2004, 08:26 PM
Thanks a lot sir, I appreciate it. I know it's an elementary facet of JavaScript, but it is surprisingly hard to find clear answers to such things on the internet, sometimes.

]|V|[agnus
06-24-2004, 10:55 PM
What is the difference here between "args" and "arguments"?

glenngv
06-25-2004, 03:59 AM
The built-in arguments property of a function is an array of all parameters passed to a function.

args is a user-defined parameter name. He could have named it with another name like arrayOfArguments. f1(args) expects an array, it so happened that you pass the arguments array in this case. You could have called it like these:

var arr = new Array('Test','This','Function');
f1(arr);
var arr2 = ['Test','This','Function'];
f1(arr2);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum