Skip to content

Instantly share code, notes, and snippets.

@ruben-h
Last active February 27, 2017 18:22
Show Gist options
  • Save ruben-h/ab1b0310cb148ff46cb7 to your computer and use it in GitHub Desktop.
Save ruben-h/ab1b0310cb148ff46cb7 to your computer and use it in GitHub Desktop.
Validation of 3 different inputs, and enabling submit button only when all are valid
/**
* Handle send later input validation, and enabling/disabling the
* submit button if all requirements are met
* (all three fields are filled out and valid).
*/
$scope.formIsValid = false;
function returnNewvalue(data){
return data.newValue;
}
var sendLaterDate = $scope.$toObservable('date')
.map(returnNewvalue)
.map(function (x) {
//Simple test to match datepicker timestamp. ex: Wed Nov 16 2015 00:00:00 GMT+0100 (CET)
return /^(\w+)\s(\w+)\s([0-9]+)\s([0-9]+)/.test(x);
});
var sendLaterHour = $scope.$toObservable('hour')
.map(returnNewvalue)
.map(function (x) {
return /^([0-1][0-9]|2[0-3])$/.test(x);
});
var sendLaterMinute = $scope.$toObservable('minute')
.map(returnNewvalue)
.map(function (x) {
return /^[0-5][0-9]$/.test(x);
});
//Both date and time should both be valid before enabling the button:
Rx.Observable.combineLatest(date, hour, minute, function (x, y, z) {
return x && y && z;
})
.subscribe(function (allValid) {
$scope.formIsValid = allValid;
if(allValid){
//Enable save to outbox button
jQuery("#sendsmsbutton")
.removeClass("lightGrayBg")
.addClass("greenBg")
.removeClass("disabled");
}else{
jQuery("#sendsmsbutton")
.removeClass("greenBg")
.addClass("lightGrayBg")
.addClass("disabled");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment