Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New at functions

Status
Not open for further replies.

bigracefan

Programmer
Apr 2, 2002
304
US
I'm trying to do a "Are you sure?" thing and it's not working the way I expect.
Here's the code where I get the alert to work:
Code:
<script type="text/javascript">
			function Validate()
			{
				var answer = confirm ("Are you sure you want reinstate?")
				if (answer)
				{//ok
				return true;
				}
				else
				//cancel
				{
				return false;
				
				}
			}
		</script>
		
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgColor="#5f9ea0" )>
<table cellSpacing="0" cellPadding="0" width="595" align="center" border="0">
<tr>
  <td colspan="3" align="middle">    
	<form METHOD="POST" ACTION="frmreinstate.asp" id="reinstatefrm" onSubmit="Validate()" name="reinstatefrm"><p>					
      <input type="hidden" id="radio1" name="radio1" value="1">
      <div align="center">
        <center>

The following, the way I thought it works, doesn't fire the alert.
Code:
<script type="text/javascript">
			function Validate()
			{
				var answer = confirm ("Are you sure you want reinstate?")
				if (answer)
				{//ok
				return true;
				}
				else
				//cancel
				{
				return false;
				
				}
			}
		</script>
		
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgColor="#5f9ea0" )>
<table cellSpacing="0" cellPadding="0" width="595" align="center" border="0">
<tr>
  <td colspan="3" align="middle">    
	<form METHOD="POST" ACTION="frmreinstate.asp" id="reinstatefrm" onSubmit="Return Validate();" name="reinstatefrm"><p>					
      <input type="hidden" id="radio1" name="radio1" value="1">
      <div align="center">
        <center>

What I'm looking for is it to continue submitting on "ok" and to not do anything if "cancel". What am I missing? Almost no hair left, pulled it all out!
 
Javascript is case sensitive, and as such Return is not a keyword. However, [!]r[/!]eturn is.
Code:
<script type="text/javascript">
            function Validate()
            {
                var answer = confirm ("Are you sure you want reinstate?")
                if (answer)
                {//ok
                return true;
                }
                else
                //cancel
                {
                return false;
                
                }
            }
        </script>
        
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgColor="#5f9ea0" )>
<table cellSpacing="0" cellPadding="0" width="595" align="center" border="0">
<tr>
  <td colspan="3" align="middle">    
    <form METHOD="POST" ACTION="frmreinstate.asp" id="reinstatefrm" onSubmit="[!]return[/!] Validate();" name="reinstatefrm"><p>                    
      <input type="hidden" id="radio1" name="radio1" value="1">
      <div align="center">
        <center>

As an aside, you can greatly shorten the length of your function since the confirm function already returns a boolean value - just return the result of the confirm function:
Code:
<script type="text/javascript">

function Validate() {
   [!]return[/!] confirm ("Are you sure you want reinstate?")[!];[/!]
}

</script>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Thanks for the short cut and pointing out the case sensitivity. I'm still having the same issue thou. When it put onSubmit="return Validate();" I don't see the alert, when I put onSubmit="Validate()" I see the alert but the submit continues on.

Code:
<script type="text/javascript">
			function Validate()
			{
				 return confirm ("Are you sure you want reinstate?");
			
			}
		</script>
		
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgColor="#5f9ea0" )>
<table cellSpacing="0" cellPadding="0" width="595" align="center" border="0">
<tr>
  <td colspan="3" align="middle">    
	<form METHOD="POST" ACTION="frmreinstate.asp" id="reinstatefrm" onSubmit="return Validate();" name="reinstatefrm"><p>
 
With what you've shown it's impossible to tell you what's wrong because nothing else you've posted suggests that there are any errors. Let's take this down to a completely basic level so I can show you a working example. Here's a test that includes a form and a submit button - nothing else. Upon submitting if you click cancel it returns to the form, if not it sends a post submission to google (which then throws up an error page cause it doesn't know what to do with the submission). Anyway, this is enough to show you how it's supposed to work. As far as why it's not working in your page - it's hard to say. However, what I'd do is put the elements into your page to mock the example I'll post below, and then begin to remove things out of your page (after backing it up of course), one at a time until you see what's making it perform incorrectly:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function validate() {
   return confirm("are you sure?");
}

</script>

<style type="text/css">

</style>
</head>
<body>

<form id="frm" action="[URL unfurl="true"]http://www.google.com"[/URL] method="post" onsubmit="return validate()">
   <input type="submit" value="click me" />
</form>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
The reason it wasnt working was again due to case
your source code says:
function Validate()...ect

Note the capital V on validate

when you were attempting to call it you were using all lowercase letters thus once you used a capital V when calling it it worked perfect.

Easy way is to develope a system and stick with it
personly I start every function with a lowercase letter then each new word with a capital
eg;
function fullPageValidation();

hope this helped
 
To:eek:p
You may very well have established a non-empty global variable named Validate. It may not be fragrantly established - that may be easier to spot, it may be implicitly established via some function with sloppy scope of variable name Validate.
To simulate it, put a fragrant non-empty global variable like this.
[tt]
<script>
var Validate="abc";
//etc...
</script>
[/tt]
Then with the most simplied page, it will be failing the onsubmit handling.
 
chasemedia, the OP had correct case in both the declaration and invoking of the function:
Code:
<script type="text/javascript">
            function [!]V[/!]alidate()
            {
                 return confirm ("Are you sure you want reinstate?");
            
            }
        </script>
        
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgColor="#5f9ea0" )>
<table cellSpacing="0" cellPadding="0" width="595" align="center" border="0">
<tr>
  <td colspan="3" align="middle">    
    <form METHOD="POST" ACTION="frmreinstate.asp" id="reinstatefrm" onSubmit="return [!]V[/!]alidate();" name="reinstatefrm"><p>

This is why the answer must lie in some code that the OP has not given. As tsuji pointed out, there may be another object on the page that shares the same name.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B> bites again.[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top