D
D
des1roer2016-04-19 10:55:57
Angular
des1roer, 2016-04-19 10:55:57

Angular form validation?

Can you show a minimally working example for number and date validation?
found this
codepen.io/sevilayha/pen/xFcdI
but again, the mvc principle seems to be violated there - the logic is in view,
and if something like this
jsfiddle.net/STEVER/n7uzrets,
then again, not directive
, probably this is the closest option to the truth
jsfiddle.net/zCUVf
but again, you need
to rewrite it, in fact, you want the following
, any numbers are valid. 12.34 \ 1,234 \ 1234
valid date 23:00:00

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
des1roer, 2016-04-19
@des1roer

something like this in draft form

<div ng-app="HelloHabr">
                <form name="userForm">
                    <input class="form-control" type="text" name="LovelyMimimi" ng-model="mimimi" mimimi/>

                    <button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Submit</button>
                </form>
                <style>                  
                    input.ng-valid {
                        background-color: #81F7F3;
                    }
                    input.ng-invalid {
                        background-color: #F78181;
                    }
                </style>

js
var app = angular.module('HelloHabr', []);

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

app.directive('mimimi', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            ctrl.$setValidity('mimimi', false);            
            ctrl.$parsers.unshift(function (mimimi) {
                mimimi = (mimimi.replace(',', "."));
                if (isNumeric(mimimi))
                {
                    ctrl.$setValidity('mimimi', true);
                    return mimimi.toUpperCase();
                }
                else
                {
                    ctrl.$setValidity('mimimi', false);
                    return undefined;
                }
            });
        }
    };
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question