I'm using a signature_pad and an email text field which both must be filled before continuing in my web application (x-ng-disabled="!checkSignatureAndEmail()"
).
The text field has a ng-model
so any changes are noticed immediately but the signature pad is plain JavaScript. So if I first enter the email and then sign in the signature pad the button won't become enabled.
Is there any way to register a listener or something like that? If yes, how'd I do this?
<div class="container">
<div class="modal fade" id="signatureDialog" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Bestellung bestätigen</h4>
</div>
<div class="modal-body">
<p>Mit ihrer Unterschrift bestätigen Sie... bla bla..</p>
<input type="text" class="form-control" x-ng-model="email">
<br>
<canvas id="signature-pad" width="700" height="150" style="border:1px solid #B0B0B0; border-radius: 4px;" x-ng-init="initSignaturePad()"></canvas>
<br>
<button type="button" class="btn btn-default" x-ng-click="signaturePad.clear()">Clear</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" x-ng-disabled="!checkSignatureAndEmail()" x-ng-click="saveOrder(orderInput)" data-dismiss="modal">Bestätigen</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Abbrechen</button>
</div>
</div>
</div>
</div>
</div>
In my controller:
Create SignaturePad:
$scope.initSignaturePad = function() {
$scope.signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
minWidth: 1.2,
maxWidth: 3,
backgroundColor: 'rgba(250, 250, 250, 1)',
penColor: 'rgb(0, 0, 100)'
});
}
Validation function:
$scope.checkSignatureAndEmail = function() {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test($scope.email) && !$scope.signaturePad.isEmpty();
};
Could you try this?
In your HTML, change x-ng-init for x-ng-mouseup event at canvas:
<canvas id="signature-pad" x-ng-mouseup="initSignaturePad()"></canvas>
And in your controller:
$scope.emptyCanvas = true;
$scope.$watch('emptyCanvas', function() {
$scope.checkSignatureAndEmail();
});
$scope.checkSignatureAndEmail = function() {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test($scope.email) && !$scope.emptyCanvas;
};
$scope.initSignaturePad = function() {
$scope.emptyCanvas = $scope.signaturePad.isEmpty();
};
This works for me, but I don't know if x-ng-mouseup fits your needs.