SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    14
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question load multiple function in javascript onsubmit using window.onload

    Hy! i am using javascript to validate different forms using separate function for each, now the problem is that when i load one function on window.onload the it will work fine but when i load multiple function then only the first one will work and the rest will not? to know what is the error i use try catch and this error will shown
    Code:
    typeerror:cannot set property 'onsubmit' of null
    this is my code...

    Code:
    //validation of registration page..
    function validate_form_registration()
    {
    
    
        document.getElementById('form_reg').onsubmit=function()
        {
            if (document.getElementById('first_name').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter first name";
                document.getElementById('first_name').focus();
                return false;
            }
    
            else if (document.getElementById('last_name').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter Last name";
                document.getElementById('last_name').focus();
                return false;
            }
            else if (document.getElementById('username').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter Username";
                document.getElementById('username').focus();
                return false;
            }
            else if (document.getElementById('mail').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter Email";
                document.getElementById('mail').focus();
                return false;
            }
            else if (document.getElementById('dept').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter your Department";
                document.getElementById('dept').focus();
                return false;
            }
            else if (document.getElementById('id_number').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter ID number";
                document.getElementById('id_number').focus();
                return false;
            }
            else if (document.getElementById('phone').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter Phone number";
                document.getElementById('phone').focus();
                return false;
            }
            else if (document.getElementById('address').value.length=="")
            {
                document.getElementById('error').innerHTML="please enter Your Adress";
                document.getElementById('address').focus();
                return false;
            }
            else
    
            {
                document.getElementById('error').innerHTML="";
                return true;
            }
    
        };
    };
    //end of registration page...
    
    //validation of login page.....
    function validate_form_login()
    {
        document.getElementById('form_log').onsubmit=function()
        {
            if(document.getElementById('username').value.length=='')
            {
                document.getElementById('error').innerHTML='Please enter username';
                document.getElementById('username').focus();
                return false;
            }
            else if(document.getElementById('password').value.length=='')
            {
                document.getElementById('error').innerHTML='Please enter password';
                document.getElementById('password').focus();
                return false;
            }
            else
            {
                document.getElementById('error').innerHTML='';
                return true;
            }
        };
    };
    //validation of application page..
    
    function validate_form_aplic()
    {
        document.getElementById('form_anous').onsubmit=function(){
            if(document.getElementById('select_item').value.length=='')
            {
                document.getElementById('error').innerHTML='Please select leave type';
                document.getElementById('select_item').focus();
                return false;
            }
           else if(document.getElementById('txtarea').value.length=='')
            {
                document.getElementById('error').innerHTML='Please enter application';
                document.getElementById('txtarea').focus();
                return false;
            }
           else if(document.getElementById('fromt').value.length=='')
            {
                document.getElementById('error').innerHTML='Please enter time';
                document.getElementById('fromt').focus();
                return false;
            }
           else if(document.getElementById('tot').value.length=='')
            {
                document.getElementById('error').innerHTML='Please enter time limit';
                document.getElementById('tot').focus();
                return false;
            }
            else
            {
                document.getElementById('error').innerHTML='';
                return true;
            }
        };
    };
    window.onload=function()
    { try {
        validate_form_aplic();
        validate_form_login();
        validate_form_registration();
    }
     catch (error)
     {
         alert(error);
     }
    };
    how to get it....
    the error mean that one of my id,s is null but when i run the single function then it will work. why it will not work on multiple function..

  2. #2
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    14
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    i got it..

    Code:
    window.addeventhandler('load',function()
    {
    some stuff here.....
    });
    it will not load all the function, but it can do what i want...

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    I'm glad you have it working, but if you place your JavaScripts at the bottom of your page, just before the closing </body> tag, then you should be able to omit using window.onload altogether.

    Also, there are a couple of things about your validation code that could be tidied up a bit / made more generic.

    If you can provide a simple working version of the form, then I'll show you how.

  4. #4
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    14
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank,s Pullo i will get it.....


Tags for this Thread

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
  •