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 Answer
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
7 Comments
Kiran Kumar
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
Kiran Kumar
And can i use check() for multiple input tag?
Naga Sai A
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 :)
Kiran Kumar
hello, i checked and it works. And we can call same check() for multiple input tags.
Naga Sai A
i think that should be different question :)
|