i new angularjs , trying write code login validation.following code check whether typed username , password correct or not. how can display "invalid username" , "invalid password" message when username or password not correct after pressing login button?i want display error messages instead of using alert.thank you.
<script> var app=angular.module("app",[]); app.controller("mainctrl",function($scope,$http,$window) { $scope.checklogin = function(result) { //$http.get("login.jsp") $http.get("login.jsp?sqlstr=select usrnm,usrpwd dbo.tblusr usrnm = '" + $scope.user_name + "'") .success(function(response) { $scope.mydata = response; if(!angular.isobject($scope.mydata[0])) alert("invalid user name"); else { if($scope.mydata[0].usrpwd!== $scope.password) alert("invalid password"); else //alert("successfully logged in"); $window.location.href = 'mainpage.html'; } }) .error(function(){ alert("resource not found"); }); }; }); </script>
something work:
<script> var app=angular.module("app",[]); app.controller("mainctrl",function($scope,$http,$window) { $scope.checklogin = function(result) { //$http.get("login.jsp") $http.get("login.jsp?sqlstr=select usrnm,usrpwd dbo.tblusr usrnm = '" + $scope.user_name + "'") .success(function(response) { $scope.mydata = response; if(!angular.isobject($scope.mydata[0])) alert("invalid user name"); else { if($scope.mydata[0].usrpwd!== $scope.password) alert("invalid password"); $scope.haserror = true; $scope.errortext = "invalid password"; else //alert("successfully logged in"); $window.location.href = 'mainpage.html'; $scope.haserror = false; $scope.errortext = ""; } }) .error(function(){ alert("resource not found"); $scope.haserror = true; }); }; }); and in html:
<div ng-show="haserror">{{errortext}}</div> edit- bad way validate passwords, looks you're passing password server. don't that, it's very, insecure. should validate hash on server, , pass authorization or rejection client. also, if you're transmitting passwords in plain text @ point, sure you're using ssl.
Comments
Post a Comment