JavaScript - How to validate a textbox in button click using JAVASCRIPT in C#>NET.

Asked By raj kumar's on 10-Apr-09 01:22 AM
hi to all

How to validate a textbox in button click using JAVASCRIPT in C#>NET.

I have the same code but it's not working. Explain me how to do correctly.


 function btn_Click ()
    {

 if(document.getElementById('txtpassword').value.length==0)

{
alert("Enter the Password");
}
else
{

alert("successfully logged in");

}
}



  <asp:Button ID="btn" runat="server" Text="submit" OnClick="btn_Click" />



by
Raj Kumar

re - Web Star replied to raj kumar's on 10-Apr-09 01:30 AM

put this line in pageload in code file

this.ibt.Attributes.Add("onClick","javascript:btn_Click();");

then it works

re - Web Star replied to raj kumar's on 10-Apr-09 01:38 AM

u can also call javascript in html page as

the html code is:
<asp:Button id="Button1" runat="
http://www.dotnetspider.com/forum/ViewForum.aspx?ForumId=948#" Text="Button"
onclick="javascript:btn_Click()"></asp:Button>

SeeThis - Kalit Sikka replied to raj kumar's on 10-Apr-09 01:44 AM

for 1st textbox
tbox = CType(e.Item.FindControl("id"), TextBox)
for 2nd textbox
tbox1 = CType(e.Item.FindControl("id1"), TextBox)
similarly for other textboxes

here id is the id of ur textbox in datagrid now by using this statement u can save the values of all the textboxes of the datagrid of a single column

now u can make checks on these values on button click
if(tbox="" or tbox1="" or tbox2="") then
response.write("<script language='javascript'>alert('enter the field');</script>
try this - sri sri replied to raj kumar's on 10-Apr-09 01:51 AM
hi,

change your code as below

 function checkLogin ()
    {

 if(document.getElementById('txtpassword').value.length==0)

{
alert("Enter the Password");
return false;
}
else
{

alert("successfully logged in");
return true;

}
}



  <asp:Button ID="btn" runat="server" Text="submit" OnClick="btn_Click" onclientclick="return checkLogin();" />
Use RequiredFieldValidator - Sakshi a replied to raj kumar's on 10-Apr-09 01:54 AM

For Null or blank validation use "RequiredFieldValidator".

For successfully login just land to the home page or your Main page.

Its not suggested to do validation for blank manually as RFV does it perfectly.

thanks... - raj kumar's replied to sri sri on 10-Apr-09 02:07 AM
end of post
thanks..... - Asked By raj kumar's on 10-Apr-09 02:07 AM
end of post
SPAM!!! - Asked By task force on 10-Apr-09 02:54 AM
DO not just copy and paste from your library of google search results...
STOP it - Asked By task force on 13-Apr-09 08:47 AM
All others are following up the quesyions, but have you ever doen that...