By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,819 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,819 IT Pros & Developers. It's quick & easy.

how to validate a form

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
an**********@hotmail.com (Techy) writes:
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.
if one is empty: alert.
Now if one is empty it should let the form to process


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.