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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Border on and off click toggle & some form validation.

    Hi there, I'm hoping this is the right forum to post in.

    Im a student, and this is my first scripting course. I've been working on my assignments and they dont ever seem to work properly. The assignment is to have this form validation happen, as well as a border onclick function. Ive had the two working fine on their own, but for some reason when I combined them, it stops working. Any ideas what my problem is here?

    Code:
    window.onload=function()
    {
    	// image border on click
    	
    	for(i = 0; i < document.images.length; i++)
    	{
    		document.images[i].onclick=function()
    		{
    		if(this.style.borderStyle = 'none') {
    			this.style.borderStyle = 'solid';}
    			else if (this.style.borderStyle != 'none'){
    				this.style.borderStyle = 'none';
    				}
    		}
    	}
    }
    
    
    // form validation
    
    function validateForm()
    
     {
    	document.forms[0].elements[0].focus();
    	document.forms[0].onsubmit=function() {
    	
    		for(i = 0; i < documents.forms[0].elements.length; i++)
    	{	
    		// username is required
    		
    		e = documents.forms[0].elements[i];
    		if(e.className.indexOf('required') != -1)
    		{
    			if(e.value.length < 1)
    			{
    				alert('Username is required!');
    				e.focus();
    				e.style.borderColor = 'red';
    				
    				return false;
    			}
    		}
    		
    		// birth year not a valid number
    		
    		if(e.className.indexOf('numeric') != -1)
    		{
    			if(isNaN(e.value))
    			{
    				alert(e.value + " is not a valid number");
    				e.select();
    				
    				return false;
    			}
    			
    		else if (e.value < 1900 || e.value > 2012)
    			{
    				alert(e.value + " is not a valid year");
    				e.focus();
    				e.style.backgroundColor = 'yellow';
    				
    				return false;
    			}
    		}
    		
    	}
    }


    any ideas would be greatly appreciated!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,568
    Thanks
    23
    Thanked 643 Times in 642 Posts
    To check out a js validate script I think we need the form it's validating. Also this is not DOM or JSON related. Wrong section.
    Here's a start. Your missing a final curly bracket '}' .
    This line
    Code:
    for(i = 0; i < documents.forms[0].elements.length; i++)
    It's document (NO S) and the code S/B
    Code:
    document.forms[0].length
    This is why we need to see the form. It will count the submit button and who knows what all.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,425
    Thanks
    17
    Thanked 272 Times in 272 Posts
    I think you're just missing a curly bracket for the onload function. I count five left hand brackets but only four right hand ones.

    If you don't have one, you need a decent highlighting text editor so you can match up brackets. I highly recommend SCite which has versions for just about any OS.


  •  

    Posting Permissions

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