Form validation with JavaScript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amir

    #1

    Form validation with JavaScript

    I'm looking for an example of form validation by JavaScript before it's
    processed by a PHP script.

    Any help will be appreciated.
  • Michael Winter

    #2
    Re: Form validation with JavaScript

    Amir wrote:
    [color=blue]
    > I'm looking for an example of form validation by JavaScript before it's
    > processed by a PHP script.[/color]

    This would be more on-topic in comp.lang.javas cript. Anyway...

    What are you trying to validate? It's a little difficult to give you
    anything useful unless that's known.

    The basic format you should be looking at is:

    <form ... onsubmit="retur n validate(this); ">


    function validate(form) {var elem = form.elements;
    /* ... */
    }

    where the controls within the form can be accessed through the elements
    collection (referenced by elem for convenience, above), and the validate
    function returns false if validation fails; true otherwise.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Amir

      #3
      Re: Form validation with JavaScript

      Michael Winter wrote: Amir wrote:

      I'm looking for an example of form validation by JavaScript before it's processed by a PHP script.

      This would be more on-topic in comp.lang.javas cript. Anyway...

      What are you trying to validate? It's a little difficult to give you anything useful unless that's known.

      The basic format you should be looking at is:

      &nbsp; &lt;form ... onsubmit="retur n validate(this); "&gt;


      &nbsp; function validate(form) {var elem = form.elements;
      &nbsp;&nbsp;&nb sp; /* ... */
      &nbsp; }

      where the controls within the form can be accessed through the elements collection (referenced by elem for convenience, above), and the validate function returns false if validation fails; true otherwise.

      Mike

      Thanks! This is exactly what I needed:

      &lt;form ... onsubmit="retur n validate(this); "&gt;

      I got the validation function, just couldn't figure out how to activate it before processing the form by a PHP script.

      -Amir

      Comment

      • mlemos

        #4
        Re: Form validation with JavaScript

        HEllo,

        on 04/26/2005 03:38 PM Amir said the following:[color=blue]
        > I'm looking for an example of form validation by JavaScript before it's
        > processed by a PHP script.[/color]

        You may want to take a look at this popular class for generating and
        validating forms that can generate the necessary Javascript for you to
        perform many kinds of common validation types on the client site. It can
        also perform the same types of validation on the server side with class
        PHP code itself.

        http://www.phpclasses.org/formsgeneration


        --

        Regards,
        Manuel Lemos

        PHP Classes - Free ready to use OOP components written in PHP
        http://www.phpclasses.org/

        PHP Reviews - Reviews of PHP books and other products
        http://www.phpclasses.org/reviews/

        Metastorage - Data object relational mapping layer generator

        Comment

        Working...