Click here to Skip to main content
13,042,630 members (77,708 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have 3 TEXTBOXES and a BUTTON in an page:
There are 3 LABELS next to the three TEXTBOXES
I want the javascript code to validate those textboxes on button click.
The way i need is on button click,all the lables next to BLANK fields becomes;I filled only one field and clicked button,then the 2 Lables next to two blank textboxes becomes visible.
when i enter on a blank textbox,the LABEL should become invisible
I used many way to do it through google search,but i didnt get the actual one

please help me to do it
Posted 17-Feb-11 19:06pm
Updated 17-Feb-11 19:18pm
arindamrudra 18-Feb-11 1:12am
Please search for the solution first and then post your question.
SAKryukov 18-Feb-11 1:16am
Is this a school assignment?
mkgoud 18-Feb-11 1:18am
Removed empty spaces
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

<script type="text/javascript">
    function validate()
            if (document.getElementById('<%=txtUname.ClientID%>').value == "")
                    document.getElementById('<%=lblUname.ClientID%>').visible = true;
                    return false;
                return true;


OnClientClick="return validate();"

For "i enter on a blank textbox,the LABEL should become invisible " part
use onfocus event and call the visible = false; it will work.
Always try first.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

What you are looking for is: Required Field Validator
Here you go:
Required Field Validator Control Sample[^]
Validation - RequiredFieldValidator[^]
ASP.NET RequiredFieldValidator Control[^]


Alternatively, you can do the same by placing your own logic and code on server side in button click.
1. On postback, button click, check for textbox content
2. If there is no text then set the label visible proerty to true
3. Repeat 1 & 2 for other 2 textboxes.
orc_orc_orc 18-Feb-11 1:35am
my vote of 5 :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3


Sandeep's answer is correct. But strictly if you want to have javascript to do this then here is the example.

You should not challenge any one, because there nothing you can say impossible.

If you are using master and client then you need to have the scripts at Master page's header, otherwise need to inject using the RegisterClientScript method.

so as an example have this at the site master's header section..

As from there the text box and labels not accessible (which are in the content page) you need to get the ID and search for it

<script type="text/javascript"">
  function validateThreeBoxes(boxID1, labelID1, boxID2, labelID2, boxID3, labelID3) {
        if (document.getElementById(boxID1).value == "") {
            document.getElementById(labelID1).style.visibility = "visible";
        if (document.getElementById(boxID2).value == "") {
            document.getElementById(labelID2).style.visibility = "visible";
        if (document.getElementById(boxID3).value == "") {
            document.getElementById(labelID3).style.visibility = "visible";

The aspx page can have like this....

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Label ID="Label1" runat="server" Text="Text1 Should Not be Empty" style="visibility:hidden"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <asp:Label ID="Label2" runat="server" Text="Text2 Should Not be Empty" style="visibility:hidden"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <asp:Label ID="Label3" runat="server" Text="Text3 Should Not be Empty" style="visibility:hidden"></asp:Label>
    <input type="button" id="Button1" runat="server"  value="Button1" />

At the page load event...

protected void Page_Load(object sender, EventArgs e)
    string textbox1Id = TextBox1.ClientID;
    string label1Id = Label1.ClientID;
    string textbox2Id = TextBox2.ClientID;
    string label2Id = Label2.ClientID;
    string textbox3Id = TextBox3.ClientID;
    string label3Id = Label3.ClientID;
    Button1.Attributes.Add("onclick", "validateThreeBoxes('" + textbox1Id + "','" + label1Id + "','"  + textbox2Id + "','" + label2Id + "','" + textbox3Id + "','" + label3Id + "');");

now you can see it working..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 18 Feb 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100