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 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form Validation and Submittal Error

    I have a form on a page that is built at runtime which enables the user to specify an image to upload and an ALT attribute. The file fields are in the format "fileupload_1", "fileupload_2", etc, and the ALT attributes are similar, but with 'alt' instead of 'fileupload'.

    I added this line to the form:
    Code:
    onsubmit="return validator(this)"
    And then
    Code:
    function validator(thisform)
    {
    	with (thisform)
    	{
    		for (var i = 0; i < elements.length; i++) {
    			if (elements[i].name.substring(0, elements[i].name.lastIndexOf("_")) == "fileupload"){
    				if (notEmpty(elements[i],"Image "+elements[i].name.substring(elements[i].name.lastIndexOf("_")+1)+" requires a file to upload")==false) {
    					elements[i].focus(); 
    					return false;
    				}
    			}
    			if (elements[i].name.substring(0, elements[i].name.lastIndexOf("_")) == "alt"){
    				if (notEmpty(elements[i],"Image "+elements[i].name.substring(elements[i].name.lastIndexOf("_")+1)+" requires an ALT text")==false) {
    					elements[i].focus(); 
    					return false;
    				}
    			}
    		}
    	}
    }
    The notEmpty function just returns true or false if the value of the field is empty or not.

    The script works if it encounters an error, but I can't get it to submit the form when it doesn't find an error. I have tried adding a 'return true;' statement, as well as forcing the form to submit after the 'for' loop but haven't had any luck. Does anyone have any suggestions?

    Cheers,
    Marty

  • #2
    New Coder
    Join Date
    Aug 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ooh solved my own problem - it works only if the file field is a real path.

    Cheers,
    Marty


  •  

    Posting Permissions

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