Connecting Tech Pros Worldwide Forums | Help | Site Map

how to validate a form

Techy
Guest
 
Posts: n/a
#1: Jul 20 '05
I have two fields in my form so called : invoice and cash


Now I want to validate this form on the client side with the help of
javascript in such a way that if one of these fields is empy an alert
box should pop up notifying the client.

Now if one is empty it should let the form to process

thanks

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: how to validate a form


anwar_server@hotmail.com (Techy) writes:
[color=blue]
> I have two fields in my form so called : invoice and cash[/color]
[color=blue]
> Now I want to validate this form on the client side with the help of
> javascript in such a way that if one of these fields is empy an alert
> box should pop up notifying the client.[/color]

if one is empty: alert.
[color=blue]
> Now if one is empty it should let the form to process[/color]

if one is empty: continuer.

Since I don't know exactly what you want, I'll give some code
that is easily changed:

To validate a function, always use this way to call the validation function:
---
<form ... onsubmit="return validate(this)">
---
The function itself is then:
---
function validate(form) {
var cashEmpty = form.elements['cash'].value == "";
var invoiceEmpty = form.elements['invoice'].value == "";
if (cashEmpty && invoiceEmpty) {
alert("Fill in at least one of the fields: Cash or Invoice.");
return false;
}
return true;
}
---
If this (error of both are empty) is not what you want, you only
need to change the condition of the if (and the alert message).

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread