www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Return false to stop submit?

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Location
    Old Sydney Town
    Posts
    6

    resolved [RESOLVED] Return false to stop submit?

    Hello

    The following snippet sits inside a function that validates various textbox fields, which are validated onsubmit. I have other calculations in other functions and I want to stop the form form submitting if those other calculations don't exist. The "total" is calculated on-the-fly, and if no items are selected, then the total box should be empty.

    Everything else is working perfectly - it's only this part giving me trouble. How do I stop it from submitting the form if no total has been calculated? I've tried different variations on this code:


    Code:
    if(document.getElementById("total").value=="") 
                       {
                            alert("No items selected!");
                            document.getElementById("qty1").focus();
                            return false;
                       }
    
                       else
                       {
                            return true;
                       }

    Thanks!
    Damien
    Last edited by dyingsun; 04-27-2009 at 04:30 PM. Reason: incomplete

  2. #2
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59
    Could you post the call to your function, please.

  3. #3
    Join Date
    Apr 2009
    Location
    Old Sydney Town
    Posts
    6
    Thanks for your quick response!

    Code:
    formX.onsubmit=fnValid;
    The rest of the function is just basic textbox validation.

    It sends ok, I just dont' want it to send if there is nothing in the subtotal box.


    Damien

  4. #4
    Join Date
    Nov 2008
    Location
    Unknown
    Posts
    59
    Give this a shot:

    Code:
    formX.onsubmit=return fnValid;
    If that doesn't work, try posting more code and I'll test it out.

  5. #5
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Quote Originally Posted by Amphidamas View Post
    Give this a shot:

    Code:
    formX.onsubmit=return fnValid;
    If that doesn't work, try posting more code and I'll test it out.
    That definitely is NOT going to work since that statement is assigning the function to be run and not actually running it.

    The original code should work correctly provided that the if statement actually satisfies the condition. You might try placing an alert in each part of the if statement just to make sure that the return false there is actually being called. You will probably find that the if(document.getElementById("total").value=="") is not matching all the conditions you expect it to (such as if there is a space in the field).

  6. #6
    Join Date
    Apr 2009
    Location
    Old Sydney Town
    Posts
    6
    Thanks Amphidamas and Stephen, I've pasted the fnValid function, calculation function, and the calls. I tried the change to the fnValid call, also tried putting it in quotes, and tried putting () after it.

    Also forgot to mention it is an external JavaScript file, if that makes a difference?



    Code:
    function fnValid() //validates user details and that radio button is selected
    {
    
    
    
    
    if(sName.value=="" || sName.value==" " || sName.value.length<3)
    {
    	alert("You MUST enter your surname properly");
    	sName.focus();
    	return false;
    	
    	
    }
    else
    {
    	var osName=("Surname: " + sName.value +"<br />");
    		
    }
    
    if(fName.value=="" || fName.value==" " || fName.value.length<3)
    {
    	alert("You MUST enter your name properly");
    	fName.focus();
    	return false;
    }
    else
    {
    	var ofName=("First name: " +fName.value +"<br />");
    
    }
    
    
    if(addr.value=="" || addr.value==" " || addr.value.length<3)
    {
    	alert("You MUST enter a valid address");
    	addr.focus();
    	return false;
    }
    
    else
    {
    	var oaddr=("Address: " +addr.value +"<br />");	
    }
    
    if(cit.value=="" || cit.value==" " || cit.value.length<3)
    {
    	alert("You MUST enter a valid city");
    	cit.focus();
    	return false;
    }
    
    else
    {
    	var ocit=("City: " +cit.value +"<br />");	
    }
    
    if(pCode.value=="" || pCode.value==" " || pCode.value.length<3)
    {
    	alert("You MUST enter a valid city");
    	pCode.focus();
    	return false;
    }
    
    else
    {
    	var opCode=("Post Code: " +pCode.value +"<br />");	
    }
    
    if(countryName.value=="" || countryName.value==" " || countryName.value.length<3)
    {
    	alert("You MUST enter a valid country");
    	countryName.focus();
    	return false;
    }
    
    else
    {
    	var ocountryName=("Country: " +countryName.value +"<br />");	
    }
    
    
    if(emailAdd.value=="" || emailAdd.value==" " || emailAdd.value.length<3 || emailAdd.value.indexOf("@")==-1 || emailAdd.value.indexOf(".")==-1)
    {
    	alert("You MUST enter a valid email address");
    	emailAdd.focus();
    	return false;
    }
    
    else
    {
    	var oemailAdd=("Email Address: " +emailAdd.value +"<br />");	
    }
    
    if(phoneNum.value=="" || phoneNum.value==" " || phoneNum.value.length<3 )
    {
    	alert("You MUST enter a valid phone number");
    	phoneNum.focus();
    	return false;
    }
    
    else
    {
    	var ophoneNum=("Phone Number: " +phoneNum.value +"<br />");	
    }
    
    //verify radio button selected
    
    		if(document.getElementById("mastercard").checked==true)
    			{
    					return true;
    			}
    
    		if(document.getElementById("bankcard").checked==true)
    			{
    					return true;
    			}
    		if(document.getElementById("visa").checked==true)
    			{
    					return true;
    			}
    		if(document.getElementById("amex").checked==true)
    			{
    					return true;
    			}
    
    
    		else
    			{
    				alert("Please select your credit card type:");
    				return false;
    				
    			}
    
    	
    	
                     if(document.getElementById("total").value=="") 
                       {
                            alert("No items selected!");
                            return false;
    			document.getElementById("qty1").focus();
                       }
    
                       else
                       {
                            return true;
                       }
    	
    	}//end fnValid

    Code:
    function fnSubCalc() //calculates order amount on the fly
    {
    subtotal.value="";
    
    if(document.getElementById("bigDay").checked==true)
    	{	
    		subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty1").value*19.95)).toFixed(2);
    		if (isNaN(subtotal.value)) //detects if Stupid Users are trying to enter something oether than digits.
    			{
    				alert("Not a valid quantity. Numbers only, please.");
    				document.getElementById("qty1").value="";
    				subtotal.value="";
    				document.getElementById("qty1").focus();
    			}
    	}
    else
    {
    		document.getElementById("qty1").value="";
    }
    
    
    
    if(document.getElementById("visitDragons").checked==true)
    
    	{	
    		subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty2").value*15.00)).toFixed(2);
    		if (isNaN(subtotal.value)) 
    			{
    				alert("Not a valid quantity. Numbers only, please.");
    				document.getElementById("qty2").value="";
    				subtotal.value="";
    				document.getElementById("qty2").focus();
    			}
    
    	}
    else
    {
    		document.getElementById("qty2").value="";
    }
    
    
    
    
    if(document.getElementById("malatharSavesTheDay").checked==true)
    
    	{	
    		subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty3").value*15.00)).toFixed(2);
    		if (isNaN(subtotal.value)) 
    			{
    				alert("Not a valid quantity. Numbers only, please.");
    				document.getElementById("qty3").value="";
    				subtotal.value="";
    				document.getElementById("qty3").focus();
    			}
    	}
    else
    {
    		document.getElementById("qty3").value="";
    }
    
    
    
    
    if(document.getElementById("adventuresInIstanbul").checked==true)
    
    
    	{	
    		subtotal.value=(Number(subtotal.value)+Number(document.getElementById("qty4").value*15.00)).toFixed(2);
    		if (isNaN(subtotal.value)) 
    			{
    				alert("Not a valid quantity. Numbers only, please.");
    				document.getElementById("qty4").value="";
    				subtotal.value="";
    				document.getElementById("qty4").focus();
    			}
    
    	}
    else
    {
    		document.getElementById("qty4").value="";
    }
    
    
    
    
    }//end fnSubCalc


    Code:
    document.getElementById("qty1").onchange=fnSubCalc; //calculating live subtotals
    document.getElementById("qty2").onchange=fnSubCalc; //calculating live subtotals
    document.getElementById("qty3").onchange=fnSubCalc; //calculating live subtotals
    document.getElementById("qty4").onchange=fnSubCalc; //calculating live subtotals

  7. #7
    Join Date
    Apr 2009
    Location
    Old Sydney Town
    Posts
    6
    Found the problem, had very intelligently forgotten to remove the return true statements I had as I started

    Thanks for your help, guys.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles