aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

JavaScript Enable/disable Button based on text in textboxes in

Aug 22, 2010
Here I will explain how to enable/disable button whenever user enter text in textboxes using JavaScript in
I have taken two textboxes and one button now I need to write functionality like to enable button if user enter text in two textboxes. For this we need to test whenever user entering text in textboxes at that time we need to raise onkeyup event for particular textbox. By default this event is not available for textbox in but we have a chance to use this event for textbox. Here I am enabling button whenever user enters text in two textboxes using JavaScript.

Design aspx page

<html xmlns="">
<head runat="server">
<script language="javascript" type="text/javascript">
//function to enable button if two textboxes contains text
function SetButtonStatus(sender, target) {
var first = document.getElementById('<%=txtfirst.ClientID %>');
var second = document.getElementById('<%=txtText.ClientID %>');
//Condition to check whether user enters text in two textboxes or not
if ((sender.value.length >= 1 && first.value.length >= 1) && (sender.value.length >= 1 && second.value.length >= 1))
document.getElementById(target).disabled = false;
document.getElementById(target).disabled = true;
<form id="form1" runat="server">
<asp:TextBox ID="txtfirst" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:TextBox ID="txtText" runat="server" onkeyup="SetButtonStatus(this,'btnButton')"></asp:TextBox>
<asp:Button ID="btnButton" runat="server" Text="Button" Enabled="false" />

Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


priya said...

thank you very much

IsmeSon said...

Thanks, I modified this example for only one text box. Since I'm using a master page for my webform, I needed to add "ctl00_MainPlaceHolder_" in front of my asp control's name for the JS to find my control.

Very useful example!

Anonymous said...

Please use document.getElementById("<%=btnSave.ClientID%>").disabled = false; to avoid java script null errors.

Unknown said...
This comment has been removed by the author.
Anonymous said...

thanku bhai...

Nisha said...

Hello sir,
I have different updatepanels to group the textboxes and dropdowns. Particular updatepanel will be visible on selection of tree view. My need is to check if value of text box is change in particular updatepanel. if date has been changed then only it should ask for saving else user can navigate without any message. I put it on textbox's "OnTextChanged" event. and function is like
function TestOnTextChange() {
document.getElementById("txtchange").Text = "yes";

but it gives error that the function is not part of the page. can you please advise how to resolve this. Many thanks in advance :)

Unknown said...

hello sir

using the one text box how to disable more then one button and textboxes

Articles Growing Knowledge said...

but i need this in inside Gridview.
How can it be implemented inside gridview

Unknown said...

Sir ,
I need it like whenever the user enters a text(its for an online quiz) and clicks on the submit button ....then if the user had entered the correct answer then, should take the user to the next page....if the user had entered the wrong answer ...then the browser must display an error message.
Please help with this....

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.