3

I'm trying to match two randomly generated numbers. I want to add ng-invalid class if result wrongly enter. In the controller this function is generating the numbers.

AngularJs

    $scope.getRandomSpan = function(){
            return Math.floor((Math.random()*6)+1);
}

HTML

<span ng-init="a=getRandomSpan()">{{ a }}</span>
<span ng-init="b=getRandomSpan()"> + {{ b }}</span>

I want to validate the numbers once user enter number in input field.

<input type="number" ng-model="captcha" required />

For example function generate two numbers 2 and 3 (equal to 5), now i want to add ng-invalid class if entered number not equal to 5. I have done this task in the controller which is validating after submit the form.

var random =  $scope.a + $scope.b;
var enteredcaptcha =  $scope.captcha;
if(random == enteredcaptcha){...}

But i don't know how to validate on the spot, can any one guide me about this I will appreciate. Thank You for guideline

1
  • i think you should use the two way databinding Commented May 4, 2016 at 7:58

2 Answers 2

2

i have updated my code and it will display message if you enter correct value otherwise nothing happen

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.getRandomSpan = function() {
    return Math.floor((Math.random() * 6) + 1);
  }
  $scope.$watch("captcha", function() {
    var random = $scope.a + $scope.b;
    var enteredcaptcha = $scope.captcha;
    if (enteredcaptcha) {
      var myEl = angular.element(document.querySelector('#ans'));
      if (random == enteredcaptcha) {
        myEl.addClass("ng-valid").removeClass("ng-invalid");
      } else {
        myEl.addClass("ng-invalid").removeClass("ng-valid");
      }
    }
  });
});
#ans {
  display: none;
}

.ng-invalid{
  display:none !important;
}

.ng-valid{
  display:inline-block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <span ng-init="a=getRandomSpan()">{{ a }}</span>
  <span ng-init="b=getRandomSpan()"> + {{ b }}</span>
  <input type="number" ng-model="captcha" />
  <span id="ans">excellent!!!!</span>
</div>

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

6 Comments

Thanks for sharing knowledge, its working fine as explain, but can we add ng-invalid class instead of alert("not equal");
where you want to add class?
On same input but i'm not using jquery, so is this possible to add class using angularjs. Thanks for guiding me
That's still not very angular-ish ;) I'd recommend either using ng-class or a custom validator (which seems overkill in this case). +1 for the effort though
@Aaron thanks for showing interest can you kindly explain.
|
1

To conditionally set a class on an element, you can use the ngClass directive. It accepts 3 different syntaxes, but one of the simplest is the following :

<input type="number" ng-model="captcha" required ng-class="{'ng-invalid': a+b!=captcha, 'ng-valid': a+b==captcha}"/>

I assume that a, b and captcha are reachable from the input's scope.

Of course since the class you want to add is ng-invalid, you're trying to do form validation. A more integrated way to do this would be to use angular's custom validation.

1 Comment

I could expand on custom validation but I'm not proficient with validators nor directives, so I think reading the documentation will be best

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.