简体   繁体   中英

Angular UI Mask Validation

Im using AngularUI Utils in attempt to mask a date input. However, when I try to test a regex against the field to validate the date format, it returns false every time.

HTML

<div class="row form-group">
  <div class="col-xs-12">
    <label>Date of Birth</label>
    <input class="form-control" ng-model="vm.dateOfBirth" ui-mask="99/99/9999" placeholder="MM/DD/YYYY" />
  </div>
</div>

AngularJS

var dateRegex = /^\d{2}\/\d{2}\/\d{4}$/;

var vm = this;
vm.dateOfBirth = "";

vm.validateInput = function () {
    return dateRegex.test(vm.dateOfBirth);
}

Is there any other way to validate this input?

I found a solution, but seems a bit hacky. Apparently if the model does not match the mask regex, the variable will be undefined or an empty string (when you backspace through the mask).

vm.validateInput = function () {
    return typeof vm.dateOfBirth !== "undefined" && vm.dateOfBirth != "";
}

This seems to do the trick, but it will only work for the very simple regular expressions that are accepted by AnguarUI masks. I would love to know if there is another way to apply additional regex validation on top of the mask like I was attempting to do originally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM