Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic Form Validation Function

    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.

    Code:
    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:

    Code:
    <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.

    Code:
    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.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Start counting at 0.....

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by ]|V|[agnus; 06-24-2004 at 05:29 PM.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    You're mixing hierarchial (object.property.property) references with element-accessing methods. This:
    Code:
    document.getElementById(arguments[i]);
    ...should get the field object by itself. So this:
    Code:
    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.
    Last edited by adios; 06-24-2004 at 05:38 PM.

  • #6
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ]|V|[agnus
    Please don't comment......
    Ok, no problem....
    I won't [open ignore].....

    Have a Nice Day.....

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roy Sinclair
    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...

  • #8
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Willy Duitt
    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.

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ]|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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roy Sinclair
    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:

    Code:
    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.

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This simple page demonstrates how to pass the arguments:

    Code:
    <!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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #12
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #13
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What is the difference here between "args" and "arguments"?

  • #14
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •