Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-23-2013, 03:35 PM   PM User | #1
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
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>
jarv is offline   Reply With Quote
Old 01-23-2013, 03:45 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Old 01-23-2013, 03:47 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 01-23-2013, 05:12 PM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 975
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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?
Logic Ali is offline   Reply With Quote
Old 01-24-2013, 02:27 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
http://www.codingforums.com/showpost...2&postcount=10

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.
Old Pedant is offline   Reply With Quote
Old 01-24-2013, 02:29 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-25-2013, 03:53 PM   PM User | #7
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
I tried adding return false;

the form still submits?!
jarv is offline   Reply With Quote
Old 01-25-2013, 04:27 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,101
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is online now   Reply With Quote
Old 01-25-2013, 05:49 PM   PM User | #9
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 975
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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()" ...>
Logic Ali is offline   Reply With Quote
Old 01-28-2013, 09:17 AM   PM User | #10
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
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>'
jarv is offline   Reply With Quote
Old 01-28-2013, 01:42 PM   PM User | #11
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
Can anyone help me here?
jarv is offline   Reply With Quote
Old 01-28-2013, 03:36 PM   PM User | #12
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 975
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 01-28-2013, 10:23 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
form validation, submit

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:29 AM.


Advertisement
Log in to turn off these ads.