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 09-10-2012, 04:46 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
function not repeat validating

Hi i wrote a function to validate my input but what is happening is that it kicks me out and alerts me to the first issue, but then once the clubname is done the
other checks do not execute and it writes to the db without validating the remaining inputs.

here is the function

Code:
<script type="text/javascript"> 

function validate_thisinfo()
{

            /* clubname */

           if (document.clubform.clubname.value.length < 1)
           {
             alert("Club Name is a required field.");
             document.clubform.clubname.focus();
             return(false);
           }

           if (document.clubform.clubname.value.length > 50)
           {
             alert("Club Name is limited to 50 char.");
             document.clubform.clubname.focus();
             return(false);
           }

            /* clubcity */
           if (document.clubform.clubcity.value.length < 1)
           {
             alert("Club City is a required field.");
             document.clubform.clubcity.focus();
             return(false);
           }

           if (document.clubform.clubcity.value.length > 50)
           {
             alert("Club City is limited to 50 char.");
             document.clubform.clubcity.focus();
             return(false);
           }

return(true);
}//close function validate_thisinfo

</script>
So what happens is that it alerts me when clubname is blank but when i input club name and leave city blank it should alert me again when i
click submit but it does not it just writes to the db without validating the city or any other info.

It should return false unless it passes all tests then returns true.


Im not sure why it is not re exicuting the function every time i click submit.

Here is the call to the function

Code:
<input type="submit" name="Submit" value="Submit" class="button" onClick="return validate_thisinfo()" />
Thanks im not sure why its not working, any help would be appreciated.

Last edited by durangod; 09-10-2012 at 10:56 PM..
durangod is offline   Reply With Quote
Old 09-10-2012, 04:59 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Change return(false) to return false;

Ditto for true.
__________________
^_^

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 09-10-2012, 05:07 PM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Changed and still doing same thing, catches the first if statment but then once that is fixed it does not execute or revalidate on click and writes to db

and all my other scripts that work use return (value); not return value; so im not sure what i missed here

Last edited by durangod; 09-10-2012 at 05:09 PM..
durangod is offline   Reply With Quote
Old 09-10-2012, 05:51 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
For form validation, you aren't returning a value. return false; prevents the form from being submit. Or, at least it's supposed to.

Have you checked the error console to see if any JavaScript error messages are being presented? If the JavaScript is breaking, this would allow the form to submit.
__________________
^_^

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 09-10-2012, 08:48 PM   PM User | #5
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
no errors i did validate with php because i want to do both javascript popups and also errorpage so the php is working but i still cant get the javascript to revalidate, pretty strange.

Im still digging, i copied a function from another script that works and same thing. Really wierd stuff.

I will keep you posted, if you have any ideas please share, maybe you have something i can post in there to test if javascript is working right.
durangod is offline   Reply With Quote
Old 09-10-2012, 09:10 PM   PM User | #6
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
I also just tried this and nothing, no reaction at all when i left the field. And yes did change the function hame to validateinfo i was easier to remember lol

Code:
<input type="text" name="clubcity" id="clubcity" maxlength="50" value="" onblur="return validateinfo();" />
Update: i did a test where i changed the onClick to another function that just checks one if statment at a time, i put one statement in there. And it worked, so the issue must be with having multiple statments...


Update 2 i think i may be onto something here, i just added another if to that function and they both worked, got two popups so now i will try adding one at a time to that function and see what happens. really really strange stuff i swear never a dull moment...

Last edited by durangod; 09-10-2012 at 09:24 PM..
durangod is offline   Reply With Quote
Old 09-10-2012, 09:43 PM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
I got it working, i had mispelled one of the vars in the function grrrrrrr im so embarrassed. Also i guess if all passes it defaults to return true because i removed the return true from the function and if all data is correct it it submits.

is that correct, does it default to true unless it is changed to false?
durangod is offline   Reply With Quote
Old 09-10-2012, 09:45 PM   PM User | #8
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
I am out of ideas. The brackets are opened/closed properly. The code looks good. I'm stumped as to why the error console is not showing any error messages. Are you using IE or FF?
__________________
^_^

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
Users who have thanked WolfShade for this post:
durangod (09-10-2012)
Old 09-10-2012, 09:45 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by durangod View Post
is that correct, does it default to true unless it is changed to false?

Yes.
__________________

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 offline   Reply With Quote
Users who have thanked Philip M for this post:
durangod (09-10-2012)
Old 09-10-2012, 09:54 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
You should attach an "onsubmit" attribute to the form rather than "onclick" to the submit button.

Code:
<form id="frmContact" action="" method="post" onsubmit="return validateinfo(this);">

function validateinfo(frm) {
    // return false to prevent form submission..
    // otherwise, do nothing and the form will submit.
}
You can still use "onblur", etc., to validate data as they complete the form but returning true/false from such events will have no affect on the form submission. I would, therefore, not use the same function for these different events.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
durangod (09-10-2012)
Old 09-10-2012, 10:20 PM   PM User | #11
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
thanks everyone im glad i finally got it, i guess i learned what happens when one part of the javascript fails, i would assume it would all fail or that i would get a message so not sure what happened because it ran the first if and then failed and the typo was on the third if statment that i added. Strange how the first one worked and then nothing else. lesson learned on several levels here.

Appreciate the help all.
durangod is offline   Reply With Quote
Reply

Bookmarks

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 09:25 AM.


Advertisement
Log in to turn off these ads.