1

I need to check that my input field is not empty.

I tried the following but it's not working.

JavaScript:

if ($scope.currentUser.Login.length != 0) { console.log($scope.currentUser.login.length); } 

HTML:

<div class="cClearFloat cInputSpace"> <input placeholder="login" ng-model="currentUser.login"> </div> 

Can someone help me?

5 Answers 5

2

try:

if($scope.currentUser.login !== null && $scope.currentUser.login !== undefined && $scope.currentUser.login !== "") { // Do stuff } 
Sign up to request clarification or add additional context in comments.

3 Comments

This is a bit of an overkill, cause null, undefined and an empty string all evaluate to false on a simple check like if($scope.currentUser.login) so this would be enough in your case. But since you want to be thorough, you would check if($scope.currentUser) before trying to access login
"0" would return false with if($scope.currentUser.login)
@HopefulLlama: Actually, if ("0") { this_is_executed(); }. "0" is a string that evaluates to true because it is not null.
1

You have a typo. Your model points to login, not Login. And to be safe you want to check if the property actually exists before you use something like length or another expression on it.

if ($scope.currentUser && $scope.currentUser.login && $scope.currentUser.login.length > 0) { console.log('the property login exists and is not empty'); } 

Comments

0

If it's a field that is always required you can use the required method.

<form action="demo_form.asp"> Username: <input type="text" name="usrname" required> <input type="submit"> </form> 

And if you want to get more specific, you can specify the length requirements.

<input pattern=".{5,10}" required title="5 to 10 characters"> 

3 Comments

Thats not really an answer to the question
it could work but not in by me because i make a new user and i save the text when its not null so :).
@MrSolution I added another part so you can specify the length requirements. That should solve your problem.
0

Is this not enough? You can just check for if($scope.currentUser && $scope.currentUser.login) It doesn't allow space, you can check the length if you want to accept blank input. And as lexith said, you bind to login and you check for Login. they are not the sames attributes.

angular.module('myModule', []); angular.module("myModule") .controller("DemoCtrl", demoCtrl); demoCtrl.$inject = ["$scope"]; //demoCtrl function demoCtrl($scope) { $scope.check = function(){ if($scope.currentUser && $scope.currentUser.login){ console.log($scope.currentUser.login); } else{ console.log("there is no login"); } } }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <html> <head> <title>AngularJS test</title> </head> <body data-ng-app="myModule"> <div data-ng-controller="DemoCtrl as demoCtrl" > <div class="cClearFloat cInputSpace"> <input placeholder="login" ng-model="currentUser.login"> </div> <button ng-click="check()">check</button> </div> </body> </html>

1 Comment

but when the input is empty, the if condition is false. What do you expect else?
-1

Add required like this

<div class="cClearFloat cInputSpace"> <input placeholder="login" ng-model="currentUser.login" required> </div> 

if it empty, the browser with notify the user.

  1. check this in controller:

if ($scope.currentUser.Login.length =="undefined") { console.log($scope.currentUser.login.length); }

Happy Coding.

2 Comments

i got the problem, that 'length' is undefined
Thats because he just copied your code (with the wrong parts). Take a look at my answer and you'll understand the reason you get undefined with your check :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.