Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: JavaScript
Expertise: Beginner
Oct 19, 2000



Application Security Testing: An Integral Part of DevOps

Form Validation

I need to perform a validation on five fields in a form. The logic is:

If field1 length >0 then disallow form submission if field2, field3, field4, and field5 are all empty. However, if any one of the fields (2-5) contain info, then submit the form.

I have tried many variations of the following code, but it still forces me to enter something in all the fields (2-5).

if ((document.addorupdate.field1.value.length >1) && 
((document.addorupdate.field2.value == "")| | 
(document.addorupdate.field3.value == "")| |
(document.addorupdate.field4.value == "")| |
(document.addorupdate.field5.value == ""))){
alert("Sorry, you must update one of the empty fields.");
return false;
else {return true ;}

Try this instead:

myForm = document.addorupdate
filledOut = false

if (myForm.field1.value != "") {
	if (myForm.field2.value != "") filledOut = true
	if (myForm.field3.value != "") filledOut = true
	if (myForm.field4.value != "") filledOut = true
	if (myForm.field5.value != "") filledOut = true

if !(filledOut)
	alert("Sorry, you must update one of the empty fields.")

return filledOut
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date