Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    form validation alerts but still submits form

    I just want to validate my form but after clicking on "OK" on the JavaScript Alert box, my form still submits?!

    Please help... below is my Javascript

    Code:
    <script>
       
    function notEmpty(){
    	var myTextField = document.getElementById('emailaddress');
    	if(myTextField.value == "")
    		alert("Please enter a valid email address");
    }
    
       function submit_vote(obj) {
       	  
    	  var vote_value = getCheckedValue(document.forms['vote-form'].elements['opt']);
          var poststr = "poll_id=" + encodeURI( document.getElementById("poll_id").value ) +
          				"&emailaddress=" + encodeURI( document.getElementById("emailaddress").value ) +
                        "&voteid=" + encodeURI( vote_value);				
          makeVote('vote.php', poststr);
       }
    </script>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,914
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    alert("Please enter a valid email address");
    return false;


    Form validation of the pattern if(myTextField.value == ") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.


    In the first book of the Bible, Guinness, God got tired of creating the world so he took the Sabbath off. - Pupil's answer to Catholic Elementary School test.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You are calling two functions that you did not include (getCheckedValue and makeVote).

    I am not sure, but I don't think var name = "data" +
    "more data" +
    "even more"; is valid or good JS. Try var name = "data";
    name += "more data";
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jarv View Post
    I just want to validate my form but after clicking on "OK" on the JavaScript Alert box, my form still submits?!
    What part of your code do you think should prevent the form from submitting?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Quote Originally Posted by WolfShade View Post
    I am not sure, but I don't think var name = "data" +
    "more data" +
    "even more"; is valid or good JS.
    WHAT???

    Pardon, but how did you come by that belief?

    Of course it's both valid and good JavaScript.

    The + operator, whether used to really do arithmetic addition or string concatenation, returns the added/concatenated value as its result. So of course you can then add or concatenate further.

    If you don't think that's legal, look at this:
    Coding a Forum Level Up System

    Look at the next to last statement in all that JS code.
    Code:
    NewCode =  topR + midR + name + ": " + sign +" "+ plevel 
            + "<BR> XP: "+ (EXP-showxp) 
            +"<BR> Experience needed: " +  (xpup-EXP)
            + "<BR>Overall Experience:" + (xneeded * EXP / xneeded) 
            +"<br><!-- placeholder --><br><br>"
    (It's not very good HTML, but it's perfectly legal and correct JavaScript code.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    If you can't string operators together to create an expression, how would you do stuff like
    Code:
    var area = 3.14159265 * radius * radius;
    var volume = width * length * height;
    var total = cost + tax + shipping + handling;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    I tried adding return false;

    the form still submits?!

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,914
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by jarv View Post
    I tried adding return false;

    the form still submits?!
    Show us your HTML <form> tag

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jarv View Post
    I tried adding return false;

    the form still submits?!
    Then you're probably doing <form onsubmit="myFunction()" ...> when it should be <form onsubmit="return myFunction()" ...>

  • #10
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    thanks that worked, now I can;t submit the form

    PHP Code:
    $showpoll '<span id="vote_msg">
                <form name="vote-form" action="javascript:submit_vote(document.getElementById(\'vote-form\'));" id="vote-form" method="post">
                <b style="font-size:13px;">'
    .$question.'</b>
                <ol id="poll-options">'
    .$options.'
                </ol>
                Email address:<br /><input type="text" name="emailaddress" id="emailaddress" /><br /><br />
                Renewal Date:<br /><input type="text" tabindex="1" class="date demo" name="renewaldate" id="renewaldate" readonly=""><br /><br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="vote" onclick=\'return notEmpty()\' id="vote" value="Submit Vote" />
                </form>
      <div align="right"><a href="javascript:getpage(\'wp-content/themes/boat-zone/result.php?poll_id='
    .$pid.'\', \'vote_msg\');" class="readmore3" id="view-poll" >View poll results</a>&nbsp;&nbsp;&nbsp;</div><br>    
                </span>'
    ;
    }else{  
        
    $showpoll '<span id="vote_msg"><p style="text-align:center">You have voted already</p>';
        
        
    $showpoll .= '<div align="right"><a href="javascript:getpage(\'wp-content/themes/boat-zone/result.php?poll_id='.$pid.'\', \'vote_msg\');" class="readmore3" id="view-poll" >View poll results</a>&nbsp;&nbsp;&nbsp;</div></span>'

  • #11
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    Can anyone help me here?

  • #12
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by jarv View Post

    action="javascript:submit_vote(document.getElementById(\'vote-form\'));"
    Who taught you to do that? That is not the purpose of the action parameter.

    Any function(s) executed on submit should be called either by the onsubmit handler or the submit button's onclick event. Preferably not both.

    Never use "javascript:", it's not necessary.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    In fact, the action= can *NOT* be JavaScript. Period.

    Code:
    <form action="vote.php" method="post" onsubmit="return submit_vote(this);" >
    But if your function submit_vote() still looks like it did in your first post in this thread, then it is COMPLETELY UNNECESSARY!

    The browser will *AUTOMATICALLY* encode all your <form> elements for you and will automatically send them as a post-date string when the <form> is submitted.

    If you are trying to do this as an AJAX call, then you are going about it all wrong. If this is the case, then show us the code for your make_vote( ) function.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    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
    •