Skip to content

Instantly share code, notes, and snippets.

@dixonsiu
Created September 4, 2017 02:10
Show Gist options
  • Save dixonsiu/bdce802b9c1f8347acfe8b4d63e2b131 to your computer and use it in GitHub Desktop.
Save dixonsiu/bdce802b9c1f8347acfe8b4d63e2b131 to your computer and use it in GitHub Desktop.
Using underscore.js methods to make validation logic simplier
function checkInput(id, msgId) {
/*
* someFieldsMissingValue is true if at least one input element's value is empty
*/
var someFieldsMissingValue = _.some(
$("form input"),
function(aDom) {
return _.isEmpty($(aDom).val());
}
);
/*
* someErrors is true if at least one element's contains error message
*/
var someErrors = _.some(
$("form .errorMsg"),
function(aDom) {
return !_.isEmpty($(aDom).html());
}
);
if (someFieldsMissingValue || someErrors) {
disableCreateBtn();
} else {
enableCreateBtn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment