Click here to Skip to main content
15,922,309 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi friends,

I'm having values as name, age, sex and country.

I want to make alert box using javascript when these field were null.

my code is
title>Untitled Page</title>
<script type="text/javascript" language="javascript">
    function Checkid ()
    {
    var name=document.getElementById('TxtName');
    if(name=="")
    {
    alert("enter value in name field");
    }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">

    <div>
    <table>
    <tr>
    <td>
    Name
    </td>
    <td>
    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Age
    </td>
    <td>
    <asp:TextBox ID="TxtAge" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Sex
    </td>
    <td>
   <asp:RadioButtonList ID="RadioBtn" runat="server" RepeatDirection="Horizontal">
   <asp:ListItem>Male</asp:ListItem>
   <asp:ListItem>Female</asp:ListItem>
   </asp:RadioButtonList>
    </td>
    </tr>
    <tr>
    <td>
    Country
    </td>
    <td>
    <asp:DropDownList ID="Drp1" runat="server">
    <asp:ListItem>India</asp:ListItem>
    <asp:ListItem>USA</asp:ListItem>
    <asp:ListItem>UK</asp:ListItem>
    <asp:ListItem>France</asp:ListItem>
    <asp:ListItem>Japan</asp:ListItem>
    <asp:ListItem>Gremany</asp:ListItem>
    </asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td colspan=2 align="center">
    <asp:Button OnClientClick="Checkid ();" ID="Btn1" Text="&nbsp;Save&nbsp;" runat="server" />
    </td>
    </tr>
    </table>

    </div>



I just want to learn java script, please suggest me the good pdf or any links with examples.

Thanks in advance
Posted
Updated 6-Jun-11 21:48pm
v4
Comments
Sergey Alexandrovich Kryukov 7-Jun-11 3:18am    
PDF?!
--SA
thatraja 7-Jun-11 3:30am    
I think he meant an e-book.
Sergey Alexandrovich Kryukov 7-Jun-11 16:18pm    
Same thing is e-book -- ?!!!!
The idea of e-books is simply a crime against humanity, and a big fraud. Don't you think so?
--SA
thatraja 7-Jun-11 16:26pm    
You mean illegal copies, right? Yeah I agree.
otherwise.... I think you are a hard copy books lover.
Sergey Alexandrovich Kryukov 7-Jun-11 17:14pm    
Why illegal? Don't you realize that e-book is a legal crime itself. A book does not belongs the them, but they copy-protect it. You can pass your paper book to your children. Do you know that inheriting e-book a landing it to a friend is considered illegal? The whole thing is one big crime.
--SA

hi,

replace your script by this . i am tested its working fine


XML
<script type="text/javascript" language="javascript">
   function Checkid ()
   {
   var name=document.getElementById('TxtName').value;
   if(name=="")
   {
   alert("enter value in name field");
   }
   }
   </script>




Happy Coding
 
Share this answer
 
Comments
beginner in C#.net 7-Jun-11 3:52am    
thanks..
i want alert for radiobutton too...
anvas kuttan 7-Jun-11 3:53am    
is it work for you you for got to add .Value ok i will post for radio button too
accept it if you got the answer
beginner in C#.net 7-Jun-11 5:24am    
i m waiting for ur radiobutton answer...
beginner in C#.net 7-Jun-11 4:08am    
tnx my 5
Monjurul Habib 7-Jun-11 5:17am    
thank you.
Plase try this function
function check()
{
 var name=document.getElementById('<%=TextBox1.ClientID%>');
   if(name.value=="")
   {
   alert("enter value in name field");
   }
}


Refer this link.....
http://www.w3schools.com/js/default.asp[^]
 
Share this answer
 
Comments
Dalek Dave 7-Jun-11 3:34am    
Good Call.
beginner in C#.net 7-Jun-11 4:07am    
tnx my 5
I think these links will help you.

http://www.w3schools.com/js/default.asp[^]
http://www.learn-javascript-tutorial.com/[^]

More here[^]

And here your problem is, you are checking name which is object to null. It is not correct. You have to check the value like this.
C#
if(name.value == "")
{
  alert("enter value in name field");
}
 
Share this answer
 
v2
Comments
Dalek Dave 7-Jun-11 3:34am    
Good Answer Toniyo.
Toniyo Jackson 7-Jun-11 3:35am    
Thanks Dave :)
beginner in C#.net 7-Jun-11 4:07am    
tnx my 5
 
Share this answer
 
Comments
beginner in C#.net 7-Jun-11 4:07am    
tnx my 5

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