1
   <input type="text" ng-model="num1">
   <input type="text" ng-model="num2">

I want to compare numbers entered in above fields and generate error message if num1 < num2 using ng-form. We can do with JS but i want to keep validation simple. Can we implement in HTML tag using ng-form

1

1 Answer 1

1

Try something like this:

<div ng-form="regForm">
   <span ng-show="num1>=num2">Num1 cannot be greater than num2</span>
   <input name="num1" type="text" ng-model="num1">
   <input name="num2" type="text" ng-model="num2">
</div>

JsFiddle: https://jsfiddle.net/sktajbir/6fmvsaf0/25/

Hope this will help you. Thanks.

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

3 Comments

This code i have tried it before but it don't work. Because it validates only one digit of number. Example num1 = 33 and num2 = 200. Still message pop up.
but according to your logic it will show error if num1 < num2. So what is wrong here?
Actually problem is we are entering numbers in text fields. And try to compare. Its taking like a string rather than numbers. "<" symbol works correctly for numbers. So we have to use parseInt(num1)

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.