1

is this form validation function correct? or it can be done better ?
For example: I want one of the two drop-downs to have a value before checking the contact details fields, (dp1 OR dp2) but even though using ( || ) the validation is acting as &&.

$('#send').click(function(){

if( document.downloadForm.SoftwareDp.value == "0" || document.downloadForm.ManualDp.value == "0" )
{
 alert( "Please Select a file for Download" );
 return false;  }

// First check if Dropdown 1 OR dropdown 2 have been selected ( ONE at least)


if( document.downloadForm.name.value == "" && document.downloadForm.email.value == "" )
{
 alert( "Please enter your details" );
 return false;  }

 // then check if name and email are typed in.

 else{ // run some ajax if above conditions are met } });

Demo : http://jsbin.com/UGotAFIL/1/edit?html,js,output Thanks

9
  • Why do you compare the values with 0 in the first if ? Commented Dec 13, 2013 at 10:07
  • You could use jquery validation plugin for form validation. It will more easy use. jqueryvalidation.org/validate Commented Dec 13, 2013 at 10:09
  • First default option <option value="0">Software</option> ect... then other options have value... Commented Dec 13, 2013 at 10:10
  • Take a look at this jsbin.com/IRiCases/1/edit why do you think that || is acting like a && ? it works fine. Commented Dec 13, 2013 at 10:15
  • @tiberiu.corbu, Thanks, but it is not. check the edited version... Commented Dec 13, 2013 at 10:25

3 Answers 3

3

Your condition working perfectly. If you want to check both drop down are not selected you should use && instead of ||.

If you need to select at least one drop down means you can you can use

 $('#send').click(function(){

if(!( document.downloadForm.SoftwareDp.value != "0" || document.downloadForm.ManualDp.value != "0" ))
{
 alert( "Please Select a file for Download" );
 return false;  }

// First check if Dropdown 1 OR dropdown 2 have been selected ( ONE at least)


if( document.downloadForm.name.value === "" && document.downloadForm.email.value === "" )
{
 alert( "Please enter your details" );
 return false;  }

 // then check if name and email are typed in.

 else{ // run some ajax if above conditions are met 
 } });

see this jsbin demo

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you correctly, you want to show the error when no dropdown has a value, that is both have no value. Change

if( document.downloadForm.SoftwareDp.value == "0" || document.downloadForm.ManualDp.value == "0" )

to either

if( document.downloadForm.SoftwareDp.value == "0" && document.downloadForm.ManualDp.value == "0" ) // both have the default value 0

or

if(!( document.downloadForm.SoftwareDp.value != "0" || document.downloadForm.ManualDp.value != "0" )) // none of them has not the default value

Comments

0

The way you are checking will always give alert unless you select other than 0 values in both.

You should do it this way:

if( document.downloadForm.SoftwareDp.value == "0" && document.downloadForm.ManualDp.value == "0" )

The above will give alert only when both are selected for 0. If one of them has other value it won't display alert.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.