-
[RESOLVED] Why is this simple code not working?
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById.('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
</head>
<body onload="loading()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
</div>
</form>
</body>
</html>
no alert box is displayed when i left the textbox blank and clicked the button :confused:
please help
-
Re: Why is this simple code not working?
Okay, first up, let's see if your method is getting called at all.
Try this:
Code:
<script language="javascript" type="text/javascript">
function validate()
{
alert("hi there");
if(document.getElementById.('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
Does that result in an alert appearing?
Are there any JavaScript errors on the page?
Gary
-
Re: Why is this simple code not working?
On another note, have you considered using the built in ASP.Net Validation controls to do this work? Seems to me like this would be a natural fit.
Gary
-
Re: Why is this simple code not working?
i think the syntax of this code is incorrect :
Code:
if(document.getElementById.('TextBox1').value=="")
please help
-
Re: Why is this simple code not working?
Code:
<script language="javascript" type="text/javascript">
function validate()
{
alert("hi there");
if(document.getElementById.('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
no.......this code also does not shows any alert box...........
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
gep13
On another note, have you considered using the built in ASP.Net Validation controls to do this work? Seems to me like this would be a natural fit.
Gary
i agree with you gep:thumb:
but i am surprised why this bit of code is not working :confused:
please help me to get it into work gep
-
Re: Why is this simple code not working?
but this bit of java script works:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function loading()
{
alert("hi there");
}
</script>
</head>
<body onload="loading()">
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
HowTo
i think the syntax of this code is incorrect :
Code:
if(document.getElementById.('TextBox1').value=="")
please help
i tell this becaues after writing upto this:
Code:
if(document.getElementById.('TextBox1')
when i clicked the "." then the value element is not shown by the intellisense:confused:
-
Re: Why is this simple code not working?
Code:
if(document.getElementById.('TextBox1').value=="")
This should be
Code:
if(document.getElementById('TextBox1').value=='')
-
Re: Why is this simple code not working?
Code:
if(document.getElementById('TextBox1').value=="")
works :)
-
Re: Why is this simple code not working?
Hey,
Okay, having looked at your code a bit closer, and loaded up the JavaScript console in Chrome, I can see two problems with this:
Code:
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById.('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
</head>
<body onload="loading()">
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
</div>
</form>
</body>
Firstly, remove the loading function from the onload event, since it isn't defined, it is causing an error.
Secondly, you have an extra . after getElementById.
It should look like this:
Code:
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
</div>
</form>
</body>
Hope that helps!!
Gary
-
Re: Why is this simple code not working?
one more thing:
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById('TextBox1').value=="")
{
alert("Please enter a value");
}
}
</script>
</head>
<body >
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="validate()" />
</div>
</form>
</body>
</html>
if the alertbox is displayed then it means that the textbox is empty and so as soon as the user clicks the ok button of the page,then i done want the page to have a post back...........
in the above code when the user clicks the ok button of the alert box then a postback takes place
how to stop this postback?
-
Re: Why is this simple code not working?
Return false if the test fails, else return true to postback
Code:
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById('TextBox1').value=="")
{
alert("Please enter a value");
return false;
}
return true;
}
</script>
</head>
<body >
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return validate()" />
</div>
</form>
-
Re: Why is this simple code not working?
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById('TextBox1').value=="" && document.getElementById('TextBox2').value=="")
{
alert("Please enter the UserId and Password");
return false;
}
else if(document.getElementById('TextBox1').value=="")
{
alert("Please enter the UserId");
return false;
}
else if(document.getElementById('TextBox2').value=="")
{
alert("Please enter the Password");
return false;
}
else
{
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClientClick="return validate()" />
</div>
</form>
</body>
</html>
please have a look at this code........is it ok
-
Re: Why is this simple code not working?
Code:
<script language="javascript" type="text/javascript">
function validate()
{
if(document.getElementById('TextBox1').value=="" && document.getElementById('TextBox2').value=="")
{
alert("Please enter the UserId and Password");
return false;
}
else if(document.getElementById('TextBox1').value=="")
{
alert("Please enter the UserId");
return false;
}
else if(document.getElementById('TextBox2').value=="")
{
alert("Please enter the Password");
return false;
}
return true;
}
</script>
-
Re: Why is this simple code not working?
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function reset()
{
document.getElementById('TextBox1').value=""
document.getElementById('TextBox2').value=""
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return reset()" />
</div>
</form>
</body>
</html>
but when i click the button2 in the above code then the two textbox values are set to empty but there is a postback happening........how to stop this?
-
Re: Why is this simple code not working?
Set the UsesubmitBehaviour=False for the button..
-
Re: Why is this simple code not working?
still i cant understand the meaning of this line:
whats the difference between
Code:
event.returnValue=false;
and please help
-
Re: Why is this simple code not working?
ok i had a look at this:
http://www.webdevelopersjournal.com/...jsevents2.html
now i am confused about this bit of code for the reset button:
Code:
function reset()
{
document.getElementById('TextBox1').value=""
document.getElementById('TextBox2').value=""
return false;
}
if i dont Set the UsesubmitBehaviour=False for the button ; then why there is a postback after resetting the textbox?
please please help
-
Re: Why is this simple code not working?
Hey,
I have to stress again, why are you going to the extreme of doing this, when all this, and more is done for you in the Validation Controls that ship with ASP.Net?!?
Gary
-
Re: Why is this simple code not working?
just for learning purpose gep........please help me to clear my concept
-
Re: Why is this simple code not working?
Bottom line is, if you are using the OnClientClick property of the button, and you are trying to prevent a PostBack from happening, then you need to have your JavaScript function return false. If it doesn't, then the PostBack will occur.
Part of the issue here, I suspect, is the fact that you have JavaScript errors in your function. Namely, the lines are not terminating with a ;.
What browser are you using for debugging your application? These JavaScript errors should be getting shown to you.
Gary
-
Re: Why is this simple code not working?
ok you suggested me to terminate the lines with a ; and i did this:
Code:
function reset()
{
document.getElementById('TextBox1').value="";
document.getElementById('TextBox2').value="";
return false;
}
<asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return reset()" />
still the button2 click causes the postback :confused:
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
gep13
What browser are you using for debugging your application?
Gary
IE7
Quote:
Originally Posted by
gep13
These JavaScript errors should be getting shown to you.
Gary
how to see the errors?
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
HowTo
how to see the errors?
http://www.testingreflections.com/node/view/4009
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
HowTo
still the button2 click causes the postback :confused:
This:
Code:
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function HowToreset() {
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Reset" OnClientClick="return HowToreset()" />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
Does not cause a PostBack.
i.e. watch the names that you are using for your functions.
Gary
-
Re: Why is this simple code not working?
Also, you might want to think about using a standing html input element, rather than a server button, if you are not actually doing anything on the server side.
Gary
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
gep13
watch the names that you are using for your functions.
Gary
i cant follow what you tried to mean with the function names :(
-
Re: Why is this simple code not working?
If you are are you simply want to Reset the values then Consider using the HTML Reset button
Code:
<input id="Reset1" type="reset"
value="reset" />
-
Re: Why is this simple code not working?
Just Found the Problem. The Function name was causing the Problem..
Change the function name...
It works
Code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function resets()
{
document.getElementById('TextBox1').value="";
document.getElementById('TextBox2').value="";
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="UserId :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Password :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" Text="Reset" OnClientClick="return resets();" />
</div>
</form>
</body>
</html>
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
danasegarane
Just Found the Problem. The Function name was causing the Problem..
Change the function name...
It works
That's what I said :)
Gary
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
HowTo
i cant follow what you tried to mean with the function names :(
You have a JavaScript function named "reset". This must be some form of reserved word in JavaScript, as when you use it, it does not function the way it should. This was shown in your other thread where you were using a function named "open".
In my case, I renamed your function to "HowToreset", and Dana renamed it to be "resets", in both cases, this made the function start working as expected.
Gary
-
Re: Why is this simple code not working?
Quote:
Originally Posted by
gep13
That's what I said :)
Gary
Let's wait for the reply :thumb:
-
Re: Why is this simple code not working?
-
Re: Why is this simple code not working?
i need a suggestion:I am working hard to learn javascript since after this i want to go for the ajax........so is it a good idea to learn javascript before going for the ajax?
-
Re: Why is this simple code not working?
Yes, I would say that it is essential, yes.
You will like need to become familiar in both JavaScript and jQuery.
Gary
-
Re: Why is this simple code not working?
what is jQuery all about?how to use it in asp.net?
-
Re: Why is this simple code not working?