View Full Version : new to JS need to validate form

12-18-2010, 08:11 PM
I am trying to make validation for a form but the bit I am stuck on now is:

<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.

Philip M
12-18-2010, 08:51 PM
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.

12-19-2010, 12:52 AM
If I use the onsubmit it does not validate in WC3

Philip M
12-19-2010, 10:11 AM
If I use the onsubmit it does not validate in WC3

Well it ought to. :) There must be some other error.

12-19-2010, 04:47 PM
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?

Philip M
12-19-2010, 05:15 PM
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().

12-19-2010, 08:05 PM
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.

Philip M
12-19-2010, 08:25 PM
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 -

<form name = "whatever" action = "something" .......... onsubmit = "return validate()">
where vaidate() is the name of the function (not the file) you wish to execute.

12-19-2010, 10:59 PM
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