SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validation returns true regardless when confirm() added

    Hi! I'm kinda young when it comes to JS, so I'd appreciate any critique at all on the script itself or the approach I've taken.

    This validation function checks to see if any of the boxes in the form are checked. (It's a for a "delete records" kind of deal.) If so, it returns true and allows the submit. If not, it returns false and does not allow the submit. However, I wanted to do an "are you sure" confirmation if boxes were checked, so I inserted an if boxes are checked, confirm(); if confirm true, return true; if confirm false, return false. When I add this, the function returns true and allows the script even if no boxes are checked. Can someone point me towards the flaw in my code or my strategy? Thank you in advance.

    Code JavaScript:
    function r_u_sure_del_series()
    	{
    	//-Get the number of checkbox elements in the form.
    	var $x=document.series_delete.elements.length-2;
     
    	//-Set false (nothing checked) as the default so
    	//-we have to prove something is checked.
    	var $checker_check=false;
     
    	/*Run through loop no. of times there are checkboxes.
    	We just need at least one to be checked, so if any are
    	checked, we set $checker_check to true.*/
    	for($i=1; $i<$x; $i++)
    		{
    		$t=document.series_delete.elements[$i].checked;
    		if($t==true)
    			{
    			$checker_check=true;
    			}
    		}
     
    	/*If we determine that there is a box checked, confirm
    	that user wants to delete (using confirm()). If yes, return
    	true on this function. If no, return false. Don't allow submit.*/
    	if($checker_check==true)
    		{
    		/*--START: If I remove . . . this chunk and replace with 
    		a simple return true, everything works.---*/
    		var $really_delete=window.confirm("Are you sure you want to delete the selected 
    		series? Any pieces that are in the series you delete will be 
    		put into No Series.");
     
    		if($really_delete==true)
    			{
    			return true;
    			}
    		else if($really_delete==false)
    			{
    			return false;
    			}
    		/*--END: If I remove . . . */
    		}
    	/*Else if we determine that nothing is checked, alert() user,
    	return false on this function, do not submit form.*/
    	else if($checker_check==false)
    		{
    		alert("No series were selected. Please select the series you want to delete.");
    		return false;
    		}
    	}

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    return (confirm("Are you sure you want to delete the selected
            series? Any pieces that are in the series you delete will be
            put into No Series."));
    ??
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm . . . it certainly makes sense and is an easier, much cleaner way for doing the same thing, but the same thing is happening using that strategy.

    Code JavaScript:
    function r_u_sure_del_series()
    	{
    	//-Get the number of checkbox elements in the form.
    	var $x=document.series_delete.elements.length-2;
     
    	//-Set false (nothing checked) as the default so
    	//-we have to prove something is checked.
    	var $checker_check=false;
     
    	/*Run through loop no. of times there are checkboxes.
    	We just need at least one to be checked, so if any are
    	checked, we set $checker_check to true.*/
    	for($i=1; $i<$x; $i++)
    		{
    		$t=document.series_delete.elements[$i].checked;
    		if($t==true)
    			{
    			$checker_check=true;
    			}
    		}
     
    	/*If we determine that there is a box checked, confirm
    	that user wants to delete (using confirm()). If yes, return
    	true on this function. If no, return false. Don't allow submit.*/
    	if($checker_check==true)
    		{
    		/*--START: If I remove . . . this chunk and replace with 
    		a simple return true, everything works.---*/
    		return (confirm("Are you sure you want to delete the selected 
    		series? Any pieces that are in the series you delete will be 
    		put into No Series."));
     
    		/*--END: If I remove . . . */
    		}
    	/*Else if we determine that nothing is checked, alert() user,
    	return false on this function, do not submit form.*/
    	else if($checker_check==false)
    		{
    		alert("No series were selected. Please select the series you want to delete.");
    		return false;
    		}
    	}

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,609
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why prefixing variables with $ ??

    Another thing ... if you do

    if (condition == true)

    then you don't need to check

    else if (condition == false)

    because if it is not true, then it must be false. (Assuming boolean states here).

    Code:
    if ($checker_check == true) {
      return (confirm("Are you sure you want to delete the selected
            series? Any pieces that are in the series you delete will be
            put into No Series.")); 
    } else {
      alert("No series were selected. Please select the series you want to delete.");
      return false;
    }
    One more thing - how are you actually calling the function? Are you using this?

    <form ..... onsubmit="return r_u_sure_del_series()">
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast 8080a's Avatar
    Join Date
    Sep 2007
    Location
    Austin, TX
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response . . .

    Your questioning my use of $ before my variables and the unnecessary else if conditional prompted me to rewrite that section without either and it worked! So, thanks for the critique. However, although my quirks are not efficient or right in line with the API, I was surprised that it did work. So, I went back and added $ back to the beginning of my variables and rewrote the needless conditional and it is still working. So, the issue is resolved but I haven't figured out what silly thing I had going on. If I can reproduce the issue and figure it out, I'll post my solution back here.

    BTW, my use of $ before variables is habit from spending most of my time with PHP. I have a hard time trying to find variables in my JS because they blend in without a prefix. (I'm so conditioned to look for $variablename.) Once I feel confident that I know my script inside and out, I will usually go snip them off. Other than it not being a standard practice, are there problems with using them?

    The extra else if conditional is just due to a simple mind.

    And yes, I am using <form onSubmit="function name" />.

    Thank you for taking a look!
    Last edited by 8080a; Jan 29, 2008 at 11:01. Reason: Forgot a detail.

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was messing around with this too. I passed the form as a parameter so you don't have to go digging through the DOM to get it, and made the checkbox checker a little less specific in case you rearrange the form or whatever.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function r_u_sure_del_series(form) {
    	var checker_check = false;
    	
    	for (var i=0; i < form.elements.length; i++) {
    		var el = form.elements[i];
    		if (checker_check = (el.type.toLowerCase() == "checkbox" && el.checked)) {
    			break;
    		}
    	}
    	
    	if (!checker_check) {
    		alert("No series were selected. Please select the series you want to delete.");
        return false;
    	}
    	
    	return confirm("Are you sure you want to delete the selected series? Any pieces that are in the series you delete will be put into No Series.");
    }
    </script>
    <body>
    <form onsubmit="return r_u_sure_del_series(this);" action="someURLthatdoesntexist">
    	<input type="text" name="somefield" value="bananas">
    	<input type="checkbox" name="serieseses" value="1">
    	<input type="checkbox" name="serieseses" value="2">
    	<input type="text" name="spannerInWorks" value="=)">
    	<input type="checkbox" name="serieseses" value="3">
    	<input type="checkbox" name="serieseses" value="4">
    	<input type="submit" value="OK">
    </form>
    </body>
    </html>

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 8080a View Post
    And yes, I am using <form onSubmit="function name" />.
    Then that was your problem. It should be:

    onsubmit="return yourfunction();"

    If you're missing the return there, then it will always submit.

    Also note, don't use camel case attributes. That is, use onsubmit instead of onSubmit.


Bookmarks

Posting Permissions

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