How to add custom validation to an AngularJS form?

后端 未结 12 1618
既然无缘
既然无缘 2020-11-22 10:07

I have a form with input fields and validation setup by adding the required attributes and such. But for some fields I need to do some extra validation. How wou

12条回答
  •  [愿得一人]
    2020-11-22 10:47

    You can use Angular-Validator.

    Example: using a function to validate a field

    
    

    Then in your controller you would have something like

    $scope.myCustomValidationFunction = function(firstName){ 
       if ( firstName === "John") {
           return true;
        }
    

    You can also do something like this:

    
    

    (where field1 field2, and field3 are scope variables. You might also want to check if the fields do not equal the empty string)

    If the field does not pass the validator then the field will be marked as invalid and the user will not be able to submit the form.

    For more use cases and examples see: https://github.com/turinggroup/angular-validator

    Disclaimer: I am the author of Angular-Validator

提交回复
热议问题