how to validate a form

T

Techy

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
 
L

Lasse Reichstein Nielsen

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top