Resources » TypeScript » Scripting Languages » Javascript

How to check Empty field using JavaScript


Posted Date: 20-Nov-2010  Last Updated:   Category: Javascript    
Author: Member Level: Silver    Points: 5


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 asp.net 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 (
document.getElementById
("<%=ctl.ClientID%>").value==""
)
{
alert("Field can not be blank");
document.getElementById("<%=ctl.ClientID%>").focus();
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)">
</asp:TextBox>


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.


Did you like this resource? Share it with your friends and show your love!

Responses to "How to check Empty field using JavaScript"

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

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India