NoDiv_NoClass
NoDiv_NoClass

Reputation: 138

AngularJS invalid or valid error message

I create an AngularJS App without < form >. But there is input field.

One of my input field is number.

<input type="number" class="input numberfield" placeholder="0" ng-change="changePrice()" ng-model="price" ng-pattern="/^(\d)+$/" name="price" required />

Now how I show the error message without from name?

<input type="number" class="input numberfield" placeholder="0" ng-change="changePrice()" ng-model="price" ng-pattern="/^(\d)+$/" name="price" required />
<span class="red" ng-show="price.$error.pattern">The price must be given in number format!</span>

When I type some any character inside the field, it show ng-invalid ng-invalid-number tag in input class (screenshot 1). When type some number it's show ng-valid class (screenshot 2). So my code is works.

But problem is that my error message does not work.

I follow this tutorial: http://codepen.io/astockwell/pen/JyCva

But I don't have form tag

screenshot 1 - ng-invalid-number

screenshot 1 - ng-invalid-number

screenshot 2 - ng-valid-number

screenshot 2 - ng-valid-number

Any idea?

Upvotes: 0

Views: 948

Answers (1)

SgtPepper43
SgtPepper43

Reputation: 544

Add a form tag.

Angular's validations only work when you have a form tag. You already know your problem is that you don't have a form tag, so add one.

If you can't add a form tag (like there's already some parent form tag somewhere), use ng-form instead, since you can nest those.

Upvotes: 2

Related Questions