Results 1 to 4 of 4

Thread: trying to solve this out more than 2 days now! heelp

  1. #1
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry error: 'element' is null or not an object

    how can i fix it?
    it has to validate id user picked more than 2 checkboxes.

    Code:
    function validate(f)
    {
       len = f.elements.length;
       var i=0;
       var count=0;
       for(i=0; i<len ; i++)
       {
             if(f.elements[i].type == "checkbox")
             {
                if (f.elements[i].checked) count++;
             }
       }
       if (count<2) {
            alert('check 2 boxes');
            return false;
       }
       return true;
    }
    [/CODE]
    Last edited by hantz; 06-27-2008 at 08:22 AM.

  2. #2
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default it need to be global

    meaning all the forms on the page will be validated with theat script

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    It seems that the function you've furnished seems to be a correct one. If you look at the parameter you've defined the function in a manner that it accepts the HTML form element (object). So there might be some problem if you pass the parameter as string (I mean the name of the form the way I've passed in the following program). Please do have a look at my program in which I pass the form name in which we would like to have a validation.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <title>Untitled Document</title>
            <style type="text/css">
            </style>
            <script type="text/javascript">
            	function validate(f){
    				var frm = document.forms[f];
    				var count = 0;
    				for(var i = 0; i < frm.elements.length; i++)	{
    					if(frm.elements[i].type == 'checkbox'){
    						if(frm.elements[i].checked) {
    							count++;
    						}						
    					}
    				}
    				if(count < 2){
                                            alert('You need to check at least two checkboxes');
    					return false;
    				}else{
                                            alert('You checked two checkboxes at least in the form. Success!!');
    					return true;
    				}
    			}        
            </script>
        </head>
        <body>
            <form name="form1">
                <div>
                    Field 1<input type="text" name="field1" />
                </div>
                <div>
                    Check1<input type="checkbox" name="checkbox1" value="1" />Check2 <input type="checkbox" name="checkbox2" value="2" />
                </div>
                <div>
                    Field 2 <input type="text" name="field2" />
                </div>
                <div>
                    Check3<input type="checkbox" name="checkbox3" value="3" />Check4 <input type="checkbox" name="checkbox4" value="3" />
                </div>
    			<div>
    				<input type="button" onclick="validate('form1');" value="validate"/>
    			</div>
            </form>
        </body>
    </html>
    There is one contradiction between the way you've defined your JS function and you've mentioned in your post.

    meaning all the forms on the page will be validated with theat script
    With your JS function either we have to run a looping construct like for, while to achieve this effect I think.

    Let me know if you need any more assistance, if possible a hyperlink would be great with your problem.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,069
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Another possibility is that with form elements like fieldset, label, legend - possibly others, there is no type attribute, so when testing, you would need to do:

    Code:
    if(f.elements[i].type && f.elements[i].type == "checkbox")
    To really be on the safe side, though this shouldn't be required with valid markup, you can even do:

    Code:
    if(f.elements[i] && f.elements[i].type && f.elements[i].type == "checkbox")
    If you want more help:

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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