JSFiddle - React, Tailwind, and code Playground

by hessam nadr

HTML

<div ng-app="app" ng-controller="Ctrl as ctrl">
    <form name="theform">
        <range ng-model="ctrl.theRange" name="myRange" required="true"></range>
    </form>
    OUTPUT: {{ ctrl.theRange }}
    <button ng-click="ctrl.theRange = '1033-100-05-11'">SET</button>
</div>

JavaScript

var app = angular.module("app",[]);

app.controller("Ctrl", function($scope) {
    this.theRange = "1111-17-8-9";
});

app.directive("range", function() {
    var ID=0;
    
    function constructRangeString(one, two, three, four) {
        var result;
        if( !one || !two || !three || !four ) {
            result = null;
        }
        return one + "-" + two + "-" + three + "-" + four;
    }
    
    return {
        require: "ngModel",
        restrict: "E",
        replace: true,
        scope: {
            name: "@"
        },
        template:
            '<div ng-form="{{ subFormName }}">' +
                '<input type="text" ng-model="one" minlength="4" maxlength="4" class="range-from" />' +
                '<input type="text" ng-model="two" minlength="2" maxlength="3" class="range-from" />' +
                '<input type="text" ng-model="three" minlength="1" maxlength="8" class="range-to" />' +
                '<input type="text" ng-model="four" minlength="1" maxlength="3" class="range-to" />' +
            '</div>',
        link: function(scope,elem,attrs,ngModel) {
            var re = /([0-9]+)\-([0-9]+)\-([0-9]+)\-([0-9]+)/;
            
            if( scope.name ) {
                scope.subFormName = scope.name;
            }
            else {
                scope.subFormName = "_range" + ID;
                ID++;
            }
            
            ngModel.$render = function() {
                var result, one, two, three, four;
                result = re.exec(ngModel.$viewValue);
                if( result ) {
                    one = result[1];
                    two = result[2];
                    three = result[3];
                    four = result[4];
                }
                scope.one = one;
                scope.two = two;
                scope.three = three;
                scope.four = four;
            };
            
            scope.$watch("one", function(newval) {
                var result =...