0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I added from tag and also added : fromname.price.$error.number insert of price.$error.pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.