menu

Questions & Answers

the issues with validation on multiple form conditions

i am trying to test he validation conditions,

this.postalCode == '' || this.countryName === '' || this.floorRoom == '' || this.newCustomerName == '' || this.streetAddress == '' || this.cityTown == '' || this.province == ''

i also have country_other and province_other

country_other and province_other can only exists if the countryName is provided as Other and province is hidden, by default they are declared so they cannot end up in undefined.

wat cnditions i need to modify to check if other is selected in the country, the country_other and province_other is filled up and validation and in other case, the country_other and province can be empty if the country is chosen as canada, because province loads the drop don to select the provinces

the code i tried is above

Answers(1) :

This will check if the countryName is "Other" and if so, it will check if the country_other and province_other fields are not empty. If the country is not "Other" it will check if the province field is not empty. This way, you make sure that country_other and province_other fields are filled when user selects "Other" as countryName and province field is not required in this case, and also if countryName is not 'Other' province field is required.

const isOtherCountry = this.countryName === 'Other';

if (this.postalCode == '' || this.countryName === '' || this.floorRoom == '' || this.newCustomerName == '' || this.streetAddress == '' || this.cityTown == '' || (!isOtherCountry && this.province == '') || (isOtherCountry && (this.country_other == '' || this.province_other == ''))){
  // validation failed
} else {
  // validation succeeded
}