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 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts

    re

    If I use the onsubmit it does not validate in WC3

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts

    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?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #7
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts
    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.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • Users who have thanked Philip M for this post:

    quartzy (12-19-2010)

  • #9
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •