I wrote a simple javascript function for validating to fields in a aspx page
function validate()
{
if (document.getElementById("<%=tbName.ClientID%>").value=="")
{
alert("Name Feild can not be blank");
document.getElementById("<%=tbName.ClientID%>").focus();
return false;
}
if (document.getElementById("<%=ddlBranch.ClientID%>").value=="SelectBranch")
{
alert("Branch Should Be Selected");
document.getElementById("<%=ddlBranch.ClientID%>").focus();
return false;
}
}
Everything worked fine. Later I linked it to aspx page like a external js file.
head runat="server">
script src="validation.js" type="text/javascript">
/script>
<title>Validations</title>
/head>
<form id="form" runat="server">
<asp:Label ID="lblName" runat="server" Text="Nmae: ">/asp:Label>
<asp:TextBox ID="tbName" runat="server" Width="130px">/asp:TextBox>
<asp:Label ID="lblBranch" runat="server" Text="Branch:">/asp:Label>
<asp:DropDownList ID="ddlBranch" runat="server" Width="107px">
<asp:ListItem>CSE</asp:ListItem>
<asp:ListItem>ECE</asp:ListItem>
<asp:ListItem>CIVIL</asp:ListItem>
<asp:ListItem>MECH</asp:ListItem>
<asp:ListItem Selected="True" Value="SelectBranch">SelectBranch</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return validate(<%=tbName.ClientID%>, <%=ddlBranch.ClientID%>)" />
</form>
and
In aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
Page.RegisterClientScriptBlock("MyScript", "<SCRIPT Language='JavaScript' src='validation.js'></SCRIPT>");
btnSubmit.Attributes.Add("onclick", "return validate()");
}
Now its giving error "Microsoft JScript runtime error: Object required".
I'm unable to know where I went wrong.