Search This Blog

Loading...

Nov 20, 2010

How to check Empty field using JavaScript


Sponsored Links:

This is very generic function which will check the value of control whether they are empty or not.

Uses: We can use this function to define the mandatory field of your application

JavaScript function

function CheckIfEmpty(ctl)
{
/*
Here ctl is the name of
the control, whose value you want to check

*/
if (
document.getElementById
("<%=ctl.ClientID%>").value==""
)
{
alert("Field can not be blank");
document.getElementById("<%=ctl.ClientID%>").focus();
return false;
}
return true;
}    

and how to call in aspx page

<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="myTextBox" runat="server" 
onblur="javascript:CheckIfEmpty(form1.TextBox1)" 
></asp:TextBox>
</div>
</form>
</body>

Above method will fire if the value of TextBox(myTextBox) is blank, in other word’s we are making this field mandatory.

Follow by Email