Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type ="text/javascript">
    function validate() {
        var firstname = document.getElementById("TextBox1").value;
        if( firstname == )
        {
          alert( " fill");
        }

    }
</script>

    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="Label1" runat="server" Text="NAME"></asp:Label>
&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;
        <asp:Button ID="Button1" runat="server" onclientclick="validate();"
            Text="Button" />

    </div>
    </form>
</body>
</html>
Posted
Comments
Black_Rose 15-Nov-14 1:40am    
"my javascript code is not working" is not enough to go.Please tell what is the script is about..what u r trying to do..what kind of output u r expecting..
Member 10874581 15-Nov-14 1:43am    
I Want...to display alert box....when user click on button when textbox empty.

A quick look says it's probably this bit:
JavaScript
if( firstname == )
Try
JavaScript
if( firstname == '')
instead...
 
Share this answer
 
This is not valid Javascript code. Right operand of the '==' operator is missing.

—SA
 
Share this answer
 
v2
You can try this
HTML
<html>
<body>
<script type='text/javascript'>
function Empty(element, AlertMessage){
	if(element.value.trim()== ""){
		alert(AlertMessage);
		element.focus();
		return false;
	}	
	alert("Textbox Validation: Successful.")
	return true;
}
</script>
<form>
Enter any value or left blank: <input type='text' id='txtBox'/>
<input type='button' 
	/>
</form>
</body>
</html>
 
Share this answer
 
v2
You are missing the '' in if condition,
Try like this,
HTML
<script language="javascript" type="text/javascript">
function Validate()
{
if (document.getElementById('<%=Textbox1.ClientID %>').value == "")
{
alert("Please Enter Your Field!");
document.getElementById('<%=Textbox1.ClientID %>').focus();
return false;
}
}
</script>

And call the Validate function in ur button,
ASP.NET
<asp:Button Id="Button1" runat="server" OnClientClick="return Validate();" Text="Button" />
 
Share this answer
 
v3

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