phantom phantom - 1 year ago 93
Javascript Question

reduce many || operators to one statement

I am currently validating a form and checking to ensure that the input boxes are not empty. To do this I am using the

method defined below. In my
statement there is a lot of repetition. How could I put the following into

if(!app.validation.empty(contestName) || !app.validation.empty(date) ||!app.validation.empty(month) ||!app.validation.empty(year) || !app.validation.empty(countryName) || !app.validation.empty(userName) || !app.validation.empty(userLastName))

into one method and hence reduce to one statement?

Answer Source

You could use app.validation.empty as callback with Array#every.

if (![contestName, date, month, year, countryName, userName, userLastName]
    .every(app.validation.empty)) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download