Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have 3 TEXTBOXES and a BUTTON in an asp.net 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 visible.ie;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
Updated 17-Feb-11 19:18pm
v2
Comments
arindamrudra 18-Feb-11 1:12am    
Please search for the solution first and then post your question.
Sergey Alexandrovich Kryukov 18-Feb-11 1:16am    
Is this a school assignment?
--SA
m@dhu 18-Feb-11 1:18am    
Removed empty spaces

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



Call

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.
 
Share this answer
 
v2
What you are looking for is: Required Field Validator
Here you go:
Required Field Validator Control Sample[^]
Validation - RequiredFieldValidator[^]
ASP.NET RequiredFieldValidator Control[^]

OR

Alternatively, you can do the same by placing your own logic and code on server side in button click.
Steps:
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.
 
Share this answer
 
Comments
Orcun Iyigun 18-Feb-11 1:35am    
my vote of 5 :)
Hi
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

XML
<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";
        }
    }

    </script>


The aspx page can have like this....


XML
<div>
<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>
</div>
<div>
<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>
</div>
<div>
<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>
</div>
    <input type="button" id="Button1" runat="server"  value="Button1" />



At the page load event...

C#
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..
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900