Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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 17-Feb-11 20:06pm
Edited 17-Feb-11 20:18pm
m@dhu29.5K
v2
Comments
arindamrudra at 18-Feb-11 1:12am
   
Please search for the solution first and then post your question.
SAKryukov at 18-Feb-11 1:16am
   
Is this a school assignment?
--SA
mkgoud at 18-Feb-11 1:18am
   
Removed empty spaces
Rate this: bad
good
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;
                    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.
  Permalink  
v2
Rate this: bad
good
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[^]
 
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.
  Permalink  
Comments
orc_orc_orc at 18-Feb-11 1:35am
   
my vote of 5 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
<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....
 

<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...
 
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..
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,130
1 DamithSL 5,089
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,647
4 Kornfeld Eliyahu Peter 4,409


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 18 Feb 2011
Copyright © CodeProject, 1999-2014
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