How to do login with hardcored data? - angularjs

I have two text fields named username, password and a hard coded array of objects with userdata:
<form action="submitLogin()">
<label class="item item-input">
<span class="input-label">Username</span>
<input type="text" ng-model="loginData.username">
</label>
<label class="item item-input">
<span class="input-label">Password</span>
<input type="password" ng-model="loginData.password">
</label>
<p style="text-align:center"ng-hide=myflag>wrong credentials</P>
<label class="item">
<button class="button button-block button-positive" type="submit">Log in</button>
</label>
</form>
And I have an array of object
$scope.userdata = [
{username:"vipin",password:"sddfds"},
{username:"vineeth",password:"vdsf"},
{username:"vishnu",password:"sdfsdfsd"}
];
How do I login?

Try this :
Login.html
<form ng-submit="submitLogin()">
<label class="item item-input">
<span class="input-label">Username</span>
<input type="text" ng-model="loginData.username">
</label>
<label class="item item-input">
<span class="input-label">Password</span>
<input type="password" ng-model="loginData.password">
</label>
<p style="text-align:center"ng-hide=myflag>wrong credentials</P>
<label class="item">
<button class="button button-block button-positive" type="submit">Log in</button>
</label>
</form>
And Controller
function ctrl($scope) {
$scope.userdata = [{
username: "vipin",
password: "sddfds"
}, {
username: "vineeth",
password: "vdsf"
}, {
username: "vishnu",
password: "sdfsdfsd"
}];
$scope.submitLogin = function() {
var addToArray=false;
for(var i=0;i<$scope.userdata.length;i++){
if($scope.userdata[i].username===$scope.loginData.username && $scope.userdata[i].password===$scope.loginData.password){
addToArray=true;
}
}
if(addToArray == true)
{
alert("login Sucessfull.!");
}
else
{
alert("invalid.!");
}
};
}
Here is the codepen Link: http://codepen.io/anon/pen/BKBpVd

<form action="submitLogin()">
<label class="item item-input">
<span class="input-label">Username</span>
<input type="text" ng-model="loginData.username">
</label>
<label class="item item-input">
<span class="input-label">Password</span>
<input type="password" ng-model="loginData.password">
</label>
<p style="text-align:center"ng-hide=myflag>wrong credentials</P>
<label class="item">
<button class="button button-block button-positive" type="submit" ng- click=”login()”>Log in</button>
</label>
JavaScript Code
$scope.userdata=[{username:"vipin",password:"sddfds"}, {username:"vineeth",password:"vdsf"},{username:"vishnu",password:"sdfsdfsd"}];
$scope.login=function(){
var login=0;
for(var i=0;i<$scope.userdata.length;i++)
{
If($scope.loginData.password==$scope.userdata[i].password && $scope.loginData.username==$scope.userdata[i].username){
login=1
}
If(login==1)
{
alert(“login successfully”);
}
}

Related

Binding not working using angular on Ionic

I'm using angular to populate a form, which can be edited. But the edits are not being captured by the scope variables.
Here's my code:
HTML code:
<div class="list">
<label class="item item-input">
<span class="input-label">Name</span>
<span>{{name}}</span>
</label>
<label class="item item-input">
<span class="input-label">Locality</span>
<span>{{locality}}</span>
</label>
<label class="item item-input">
<span class="input-label">City</span>
<span>{{city}}</span>
</label>
<label class="item item-input">
<span class="input-label">Amenities</span>
<input type="text" placeholder="Amenities" ng-model="amenities">
</label>
<label class="item item-input">
<span class="input-label">Total Rooms</span>
<input type="text" placeholder="Total Rooms" ng-model="trooms">
</label>
</div>
<div class="text-center">
<button class="button button-positive" ng-click="hotelUpdate()">
Save Edits
</button>
</div>
hotelUpdate() :
$scope.hotelUpdate = function() {
var hotel1 = {
objectId: $state.params.hotelId,
amenities: $scope.amenities,
free_rooms: $scope.frooms,
total_rooms: $scope.trooms
}
Backendless.Data.of( "Hotels" ).save(hotel1)
.then(function(savedObject){
console.log(savedObject);
})
.catch(function(error){
console.log(error);
})
}
}
Is there something wrong with the code? On using binding previously the same way, it has worked fine.
Before the function cal,declare a scope variable $scope.hotel = {},
In HTML code,change ng-model like ng-model = "hotel.amenities",
In controller,change the code to
var hotel1 = {
objectId: $state.params.hotelId,
amenities: $scope.hotel.amenities,
free_rooms: $scope.hotel.frooms,
total_rooms: $scope.hotel.trooms
}

Having issues with my ionic login app

i am trying to create a login application with create account option.When i run the ionic serve i get a white screen.
Heres code i have done so far
(am totally new to this stuff )
index.html
<body ng-app="starter">
<ion-nav-view></ion-nav-view>
</body>
login.html
<ion-view>
<ion-content>
<div class="login-image">
<img src="../img/img3.jpg" width="100%" class="padding">
</div>
<div class="padding">
<form ng-submit="doLogin()" >
<div class="list">
<label class="item item-input">
<span class="input-label"></span>
<input type="text" ng-model="loginData.username" placeholder="Username">
</label><br>
<label class="item item-input">
<span class="input-label"></span>
<input type="password" ng-model="loginData.password" placeholder="Password">
</label><br>
<div style=" text-align: right;">
Forgot Password
</div><br>
<label>
<button class="button button-block button-positive" type="submit" href = "template/dashboard.html">Log in</button>
</label>
<br>
<div style=" text-align: center;">
Create Account
</div>
</div>
</form>
</div>
</ion-content>
</ion-view>
createaccount.html
<ion-view>
<ion-content>
<div>
<form name="createaccountform" class="padding">
<div>
<label class="item item-input item-floating-label">
<span class="input-label">First Name</span>
<input type="text" name="firstname" placeholder="First Name">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Last Name</span>
<input type="text" name="lastname" placeholder="Last Name">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Email</span>
<input type="email" name="email" placeholder="Email">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Username</span>
<input type="text" name="Username" placeholder="Username">
</label>
<label class="item item-input item-floating-label">
<span class="input-label">Password</span>
<input type="password" name="password" placeholder="Password">
</label>
<label>
<button class="button button-block button-positive" type="submit" href="template/login.html">Create Account </button>
</label>
</div>
</form>
</div>
</ion-content>
</ion-view>
app.js
.config(function($stateProvider,$urlRouterProvider){
$stateProvider
.state('index',{
url: '/index',
abstract: true,
templateUrl: '/index.html'
})
.state('login',{
url: '/login',
templateUrl: 'templates/login.html'
})
.state('createaccount',{
url: '/createaccount',
templateUrl: 'templates/createaccount.html'
});
//allows for fallback when page not found or not exist
$urlRouterProvider.otherwise('templates/login.html');
});
Dont know what am dong wrong but i really need a guide on this.
After of run your command, need to access to the following URL to see your login page:
http://localhost:8000/login
The routing is handled in your app.js file throughout state manager.
I suggest taking at look at the official documentation to understand how works.

In console error showing ignore attempt to cancel a touchend

I design on form for user registration in ionic app, run in debug mode on phone and fill form and when I'm submitting form application not responding anything else and when I'm checking it on console error is showing like
"Ignored attempt to cancel a touchend event with cancelable=false, for
example because scrolling is in progress and cannot be interrupted."
<ion-view view-title="Sign Up" class="login-main">
<ion-header-bar>
<h1 class="title">Sign Up</h1>
</ion-header-bar>
<ion-content>
<div class="row">
<div class="login-both-fields">
<div class="list">
<form class="padding" id="signup-form" ng-submit="signup(signupForm.$valid)" name="signupForm" novalidate>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-person"></i></span>
<input type="text" placeholder="Name*" name="m_name" ng-model="mSignupData.m_name" required>
<span ng-show="submitted && signupForm.m_name.$error.required" class="popup-validation-signup">
Please enter name
</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-person"></i></span>
<input type="text" placeholder="Surename*" name="m_surename" ng-model="mSignupData.m_surename" required>
<span ng-show="submitted && signupForm.m_surename.$error.required" class="popup-validation-signup">
Please enter surename
</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-ios-location"></i></span>
<input type="text" placeholder="Address*" name="m_address" ng-model="mSignupData.m_address" required>
<span ng-show="submitted && signupForm.m_address.$error.required" class="popup-validation-signup">Please enter address</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-calendar"></i></span>
<input type="date" placeholder="Date Of Birth*" name="m_dob" ng-model="mSignupData.m_dob" required>
<span ng-show="submitted && signupForm.m_dob.$error.required" class="popup-validation-signup">Please enter DOB</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-iphone"></i></span>
<input type="number" placeholder="Mobile Number" name="m_mobno" ng-model="mSignupData.m_mobno" required>
<span ng-show="submitted && signupForm.m_mobno.$error.required" class="popup-validation-signup">Please enter mobile no</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-ios-email"></i></span>
<input type="email" placeholder="Email*" name="m_email" ng-model="mSignupData.m_email" required>
<span ng-show="submitted && signupForm.m_email.$error.required" class="popup-validation-signup">Please enter emailid</span>
<span ng-show="submitted && signupForm.m_email.$error.email" class="popup-validation-signup">Please enter valid emailid</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-person"></i></span>
<input type="text" placeholder="User Name*" name="m_username" ng-model="mSignupData.m_username" required>
<span ng-show="submitted && signupForm.m_username.$error.required" class="popup-validation-signup">Please enter username</span>
</label>
<label class="item item-input">
<span class="input-label login-input-icon"><i class="ion-locked"></i></span>
<input type="password" placeholder="Password*" name="m_pwd" ng-model="mSignupData.m_pwd" ng-minlength="8" required>
<span ng-show="submitted && signupForm.m_pwd.$error.required" class="popup-validation-signup">Please enter password</span>
<span ng-show="submitted && loginForm.m_pwd.$error.minlength" class="validation-login popup-forvalidation correct-password-login-error">Password should be min 8 character.</span>
</label>
<div class="term-line-signup">
<input id="terms" type="checkbox" name="vehicle" value="Bike" ng-model="mSignupData.vehicle" required> <label for="terms" class="lable-readinfo">By Accept</label> terms& conditions.
<span ng-show="submitted && signupForm.vehicle.$error.required" class="popup-validation-signup please-confirm-condtion">Please accept terms&condition</span>
</div>
<button type="submit" ng-click="submitted = true" class="button button-block button-positive button-energized client-btn">Register</button>
<a class="button button-block button-positive Normal-btn" href="#/mLogin">Signin</a>
</form>
</div>
</div>
</div>
</ion-content>
</ion-view>
You can try return true from touchend and everything will work fine.
Do like this :
var scroll=false;
//while moving
$("body").on("touchmove", function(){
scroll = true;
});
//at the end of touch
$("body").on("touchend", function(){
if (scroll)
return;
// wasn't a drag, just a tap
// more code here
});
//when touch starts
$("body").on("touchstart", function(){
scroll = false;
});

LocalStorage fire twice to store Object

I am try to store an object into LocalStorage, but when I triggersetItem, its store two objects (duplicate). Below are my codes;
html
<form class="clearBoth" ng-submit="addProject(projectInfo)">
<div class="list" ng-model="projectInfo">
<label class="item item-input item-stacked-label">
<span class="input-label">Project Name</span>
<input type="text" ng-model="projectInfo.name" placeholder="PSN" required>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Host</span>
<input type="text" ng-model="projectInfo.host" placeholder="http://psn.com.my" required>
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Instance</span>
<input type="text" ng-model="projectInfo.instance" placeholder="PSN" required>
</label>
</div>
<button class="button button-block button-positive" ng-click="addProject(projectInfo)">Block Button</button>
<button class="button button-block button-assertive" ng-click="removeProject()">Block Button</button>
</form>
controller
.controller("ProjectAddCtrl", function($scope) {
$scope.addProject = function (projectInfo) {
var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
$scope.projects = [{
name: "",
host: "",
instance: ""
}];
oldItems.push(projectInfo);
localStorage.setItem('itemsArray', JSON.stringify(oldItems));
};
$scope.removeProject = function () { localStorage.clear(); };
console.log(JSON.parse(localStorage.getItem('itemsArray')));
})
Both ng-click on the <button> and ng-submit on the <form> are being triggered and hence, you are seeing the same data being added twice. You can fix it by adding type="button" attribute on the first button.
Check documentation - the default value for the type attribute is submit and hence, both the click and submit events are being fired.

Clear the text fields and highlight color in angular js at the time of backside navigation?

.controller('SinginCtrl', function($scope, $http, $location, $ionicHistory, $ionicPopup, $ionicLoading, $rootScope, BuynowFactory, $timeout) {
alert("sign");
$scope.myGoBack = function() {
if ($ionicHistory.backView() == null) {
$location.path("/app/home");
};
$ionicHistory.goBack();
};
$scope.inputType = 'password';
$scope.errorMessage = '';
$scope.signIn = function(authorizationForm) {
$ionicLoading.show({
content: 'Loading',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 200,
showDelay: 0
});
var email = authorizationForm.emailId;
var password = authorizationForm.password;
var data = {
emailId: email,
passwordId: password
};
$http.post('http://www.otcdeal.com/OTCDealWS/OTCDealLoginService.php', data).then(function successCallback(response) {
$timeout(function() {
$ionicLoading.hide();
}, 0);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="RegistrationForm" ng-submit="registrationDetails(RegistrationDetails)" novalidate>
<h3> REGISTER WITH US </h3>
<div class="list">
<label class="item item-input item-stacked-label min-margin" ng-class="{'has-errors':RegistrationForm.UserName.$invalid && RegistrationForm.UserName.$touched}">
<span class="input-label">Your Name</span>
<input type="text" placeholder="User Name" ng-model="RegistrationDetails.UserName" name="UserName" required>
</label>
<p class="message-error" ng-show="RegistrationForm.UserName.$error.required && RegistrationForm.UserName.$touched">user name is required</p>
<label class="item item-input item-stacked-label" ng-class="{'has-errors':RegistrationForm.LastName.$invalid && RegistrationForm.LastName.$touched}">
<span class="input-label">Last Name</span>
<input type="text" placeholder="Last Name" ng-model="RegistrationDetails.LastName" name="LastName" required>
</label>
<p class="message-error" ng-show="RegistrationForm.LastName.$error.required && RegistrationForm.LastName.$touched">LastName is required</p>
<label class="item item-input item-stacked-label" ng-class="{'has-errors':RegistrationForm.EmailId.$invalid && RegistrationForm.EmailId.$touched}">
<span class="input-label">Email</span>
<input type="email" placeholder="username#domain.com" ng-model="RegistrationDetails.EmailId" name="EmailId" required>
</label>
<p class="message-error" ng-show="RegistrationForm.EmailId.$error.required && RegistrationForm.EmailId.$touched">user name is required</p>
<p class="message-error" ng-show="RegistrationForm.EmailId.$error.email && RegistrationForm.EmailId.$touched">please enter valid email address</p>
<label class="item item-input item-stacked-label" ng-class="{'has-errors': RegistrationForm.Password.$invalid && RegistrationForm.Password.$touched}">
<span class="input-label">Password</span>
<input type="{{inputType}}" placeholder="******" ng-minlength="6" ng-model="RegistrationDetails.Password" name="Password" required>
</label>
<p class="message-error" ng-show="RegistrationForm.Password.$error.required && RegistrationForm.Password.$touched">password is required</p>
<p class="message-error" ng-show="RegistrationForm.Password.$error.minlength && RegistrationForm.Password.$touched">Enter password as minimum 6 or more</p>
<ion-checkbox ng-click="hideShowPassword()" ng-model="filter.blue">Show Password</ion-checkbox>
<p class="message-error">{{Useravailable}}</p>
</div>
<button class="button button-block button-positive" ng-disabled="RegistrationForm.$invalid">SIGNUP</button>
</form>
enter image description hereIn My app first three views
Language view (English,arabic buttons in page)
Sign-view (sign form and have a button create an account(Register) in a page)
Register view(Register form)
sign form controller code:
<label class="item item-input item-stacked-label" ng-class="{'has-errors':LoginForm.MobileNumber.$invalid && LoginForm.MobileNumber.$touched}">
<span class="input-label">Mobile Number</span>
how to clear the has error class and fileds as normal when return from register view to sign view
cache-view="false"
This above line is solved my answer

Resources