2

I have a html code, how can i check if the input field is empty in the script. Is there any way we can use scope to check this. I know we have form validation in angular but am just curious.

1
  • 1
    Could you please share sample code which you have tried ? Commented Jul 11, 2016 at 4:41

1 Answer 1

1

To achieve expected result, you can use ng-keyup function

HTML:

<html lang="en">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form>
    First Name: <input type="text" ng-keyup="check()" ng-model="firstname">
  </form>
</div>

</body>
</html>

JS:

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.check= function(){
      var x =$scope.firstname;
      if(x.length==''){
        alert("input empty");
      }
    };
});

Codepen- http://codepen.io/nagasai/pen/qNPJZV

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

7 Comments

Hello, check() gets triggered every time the input is made empty? Am asking this because, i want check() to be called when the form loads
And can i use check() for multiple input tag?
yes you can use it multiple input fields and updated codepen which works for both on input and on form load ..Hope this works for you :)
hello, i checked and it works. And we can call same check() for multiple input tags.
i think that should be different question :)
|

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.