Problems with the ngPattern Directive

I have this code:

<form name="test">
    <input type="text" ng-model="code" name="code" ng-pattern="/^code_[0-9]+$/"/>
    <div ng-show="test.code.$dirty && test.code.$invalid">
        Error:
        <span ng-show="test.code.$error.pattern">Pattern fail.</span>
    </div>
</form>

I have 2 problems with this code:

  • Lines of type "code_232", "code_232", "code_232" are being tested. I am not a regex expert, so this may be a problem with the regex that I wrote:/^code_[0-9]+$/

  • If I start writing "code_23892" (the correct line), I get an error while I am still writing ( test.code.$error.pattern = true). Is there a built-in way to avoid this? Therefore my behavior:

If I write "cod" and the input is still focused: no error

If I write "cod" and the input loses focus: error.

If I write "a", "ca", "coa", etc .: an error, cause a template violation.

Is this possible, or have I written a custome verification directive to achieve this?

.

+5
1

, , , ng-model . , ng-trim="false" <input />. , ng-trim - , Angular.

; , , , , . , - ng-blur ng-focus , .

+9

All Articles