Last active
February 27, 2017 18:22
-
-
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
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
/** | |
* 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