Created
September 4, 2017 02:10
-
-
Save dixonsiu/bdce802b9c1f8347acfe8b4d63e2b131 to your computer and use it in GitHub Desktop.
Using underscore.js methods to make validation logic simplier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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