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 12-18-2010, 07:11 PM   PM User | #1
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
new to JS need to validate form

I am trying to make validation for a form but the bit I am stuck on now is:
Code:
<input class="submit" type="submit" value="Submit" id="submit" name="submit" onclick="return validate();" />
Do I have to add 'verify' instead of the 'validate' I mean is this a set thing in JS as I want it to go to the JavaScript code which is validate.js I should add that the form is XHTML and there is also a captcha with it. So I would not know how to add the onsubmit to the beginning of the form.
quartzy is offline   Reply With Quote
Old 12-18-2010, 07:51 PM   PM User | #2
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
Rather than attach the event onclick="return validate()" to the submit button you should use instead:-

<form name = "whatever" action = "something" .......... onsubmit = "return validate()">
The effect will be to run the function named validate() when the form is submitted and return true or false as determined by the outcome of the validation. If true is returned the form submits, if false is returned the submit is cancelled.

Form validation has been covered many times and in great depth in this forum.

Note that validate.js is a file containing JavaScript code; a validation function might well be named validate().

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
Philip M is offline   Reply With Quote
Old 12-18-2010, 11:52 PM   PM User | #3
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
re

If I use the onsubmit it does not validate in WC3
quartzy is offline   Reply With Quote
Old 12-19-2010, 09:11 AM   PM User | #4
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 quartzy View Post
If I use the onsubmit it does not validate in WC3
Well it ought to. There must be some other error.
Philip M is offline   Reply With Quote
Old 12-19-2010, 03:47 PM   PM User | #5
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
Re

My code editor is now accepting your way.
But should I put:

onsubmit=return "js_files/validate.js()"

as how will it know where the file is?
quartzy is offline   Reply With Quote
Old 12-19-2010, 04:15 PM   PM User | #6
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
The script file named validate.js which contains the various functions should have been loaded in the <head> of your document. Until then it is not available to your page.

<script type = "text/javascript" src = "js_files/validate.js"></script>

If you have also a function named validate() (which is not a very good idea as it is bound to cause sort of confusion you are experiencing) then, as I say,

<form name = "whatever" action = "something" .......... onsubmit = "return validate()">

If I were you I would rename the function to say validateMyForm().

Last edited by Philip M; 12-19-2010 at 04:17 PM..
Philip M is offline   Reply With Quote
Old 12-19-2010, 07:05 PM   PM User | #7
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
NO I dont have a function named validate but I wanted it to process the file validate.js where I will have lots of functions to validate the form. Shoud I instead make the file start of with a return function that I could name in the XHTML? It is all very confusing.
quartzy is offline   Reply With Quote
Old 12-19-2010, 07:25 PM   PM User | #8
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
I am afraid that I really do not understand you - I am as confused as you claim to be!

You say "lots of functions to validate the form". But it is sensible to have only one (which may indeed call other functions en route).

One more time -
Code:
<form name = "whatever" action = "something" .......... onsubmit = "return validate()">
where vaidate() is the name of the function (not the file) you wish to execute.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
quartzy (12-19-2010)
Old 12-19-2010, 09:59 PM   PM User | #9
quartzy
Regular Coder

 
Join Date: May 2009
Posts: 813
Thanks: 123
Thanked 24 Times in 24 Posts
quartzy is an unknown quantity at this point
OK I understand now, the first function must be the one that is called in the onsubmit button, even if that function then goes into others. Thanks
quartzy 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 03:22 AM.


Advertisement
Log in to turn off these ads.