Connecting Tech Pros Worldwide Forums | Help | Site Map

Form validation with JavaScript

Amir
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

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.javascript. 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="return 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.
Amir
Guest
 
Posts: n/a
#3: Jul 17 '05

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.javascript. 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="return validate(this);"&gt;


&nbsp; function validate(form) {var elem = form.elements;
&nbsp;&nbsp;&nbsp; /* ... */
&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="return 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
mlemos
Guest
 
Posts: n/a
#4: Jul 17 '05

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
http://www.meta-language.net/metastorage.html
Closed Thread