Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
typeerror:cannot set property 'onsubmit' of null


this is my code...

C#
//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 solve it? why the window.onload can overwrite the first function on all the other,s? is there a way to solve it?
Posted

1 solution

You do not tell us on which of the three occurrences of onsubmit you got the error, but obviously you have to us debugger...
One - or more - of the getElementById returns null. Use debugger to see which and check the corresponding element in your HTML page...
 
Share this answer
 
Comments
msz900 9-Jun-14 8:41am    
how to do that?
Kornfeld Eliyahu Peter 9-Jun-14 13:29pm    
You mean how to debug? Open up the documentation for your browser!!!
msz900 9-Jun-14 21:25pm    
i check it out, and it will show the second and third function null except the first.
what can i do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900