I have two option of JavaScript validation .Among these which one better to use for validation
Javascript validation based on FIELD NAME
if(document.formname.uname.value=="")
{
alert("Please Enter Username");
document.formname.uname.focus();
return false;
}
Javascript validation based on ID
if(document.getElementById('uname').value=="")
{
alert("Please Enter Username");
document.getElementById('uname').focus();
return false;
}
In my opinion the first option with
document.fromname.unameis better. Using IDs is like a globale variable. It is better to use the namespace of the form to avoid conflicts.Also you only have to set the name attribute of the input field instead of name and id.