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…
//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…