How to check Empty field using JavaScript

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

How to Check Empty String Using JavaScript

Below function is useful to check the value of control whether they are containing any value or not. In Other words if you want to make any fields mandatory for the user then you can use this code.

First of all you have to declare one function either in your .aspx page or you can add JavaScript file in your project.

How to Create function in JavaScript

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

if (
alert("Field can not be blank");
return false;
return true;

after function declaration in your aspx add one text box as below

<asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>

How to call/use

I am calling this function in onblur event of Textbox as

<asp:TextBox ID="myTextBox" runat="server" onblur="javascript:CheckIfEmpty(form1.TextBox1)">

That's It compile your program, browse it. Once the page is loaded, click in the Textbox then press the tab button without writing anything, here your function will call and it will prompt one message to user "Field Cannot be blank"

How to debug
add debugger keyword in your JavaScript method and run the program, whenever the JavaScript method get the call it will confirm to start debugging make sure JavaScript debugging is enabled.


No responses found. Be the first to comment...