Angular Js Validation on Partial Views - angularjs

Index Page
#model AngularMVC.Models.Employee
#{
ViewBag.Title = "Home Page";
}
<style>
input.ng-touched.ng-invalid {
border: 1px solid red;
}
input.ng-touched.ng-valid
{
border: 1px solid #48bb00;
}
li{
border:1px solid blue;
font-size:2em;
color:Green;
}
</style>
<script src="~/Scripts/angular.min.js"></script>
<script src="~/Scripts/angular-route.min.js"></script>
<script src="~/Scripts/Custom/modules.js"></script>
<div ng-app="myApp">
<form ng-controller="validateCtrl"
name="myForm" novalidate ng-submit="submit()">
<span ng-show="isViewLoading">
<img src="~/Content/images/box.gif" /> loading...
</span>
<div class="{{status}}">{{message}}</div>
<p>
<b>Name:</b> <br>
#Html.TextBoxFor(m => m.Name, new { #class = "form-control", #ng_required = "true", ng_model = "Model.name", Name = "name" })
<span style="color:red" ng-show="myForm.name.$dirty && myForm.name.$invalid">
<span ng-show="myForm.name.$error.required">Username is required.</span>
</span>
</p>
<p>
<b>Email:</b><br>
#Html.TextBoxFor(m => m.Email, new { #class = "form-control", #ng_required = "true", ng_model = "Model.email", Name = "email", type = "email" })
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>
<p>
<b>Password:</b><br />
#Html.PasswordFor(m => m.Password, new { ng_model = "Model.password", #ng_required = "true", #class = "form-control", ng_minlength = "8", ng_maxlength = "16", Name = "password" })
<span style="color:red" ng-show="myForm.password.$dirty && myForm.password.$invalid">
<span ng-show="myForm.pass.$error.required">Password is required.</span>
<span ng-show="myForm.password.$touched && myForm.password.$error.minlength">Passord must be 8-16 character</span>
<span ng-show="myForm.password.$touched && myForm.password.$error.maxlength">Passord must be 8-16 character</span>
</span>
</p>
<p>
<input type="submit" ng-disabled="myForm.$invalid" value="Submit">
</p>
</form>
#*<p>Main</p>
First*#
Second
<div class="panel panel-default">
<div class="panel-heading">
{{PanelHeading}}
</div>
<div ng-view class="panel-body">
</div>
</div>
</div>
Module.js
var app = angular.module('myApp', ['ngRoute']);
app.controller('validateCtrl', function ($scope, $http) {
$scope.submit = function () {
//$scope.model = #Html.Raw(Json.Encode(Model));
$scope.isViewLoading = true;
$http({
method: "post",
url: "/Home/Create",
data: { emp: $scope.Model }
}).success(function (data, status, header, config) {
if (data = "Success") {
$scope.status = "text-success";
$scope.message = "Data successfully submitted";
}
else {
$scope.status = "text-warning";
$scope.message = "Failed to insert Data";
}
}).error(function (data, status, header, config) {
$scope.status = "text-danger";
$scope.message = 'Unexpected Error while saving data!!';
});
$scope.isViewLoading = false;
}
});
// Routes Provider for SPA
app.config(function ($routeProvider) {
$routeProvider
.when('/first', {
templateUrl: '/Home/first',
controller: 'HomeController',
})
.when('/second', {
templateUrl: '/Home/Second',
controller: 'HomeController'
})
});
Partial View Having a signup form
<div>
<form action="/Home/SignUp" name="signupForm">
<p>
<b>First Name:</b><br />
<input type="text" class="form-control" ng-required="true" ng-model="fname" name="fname" />
<span ng-show="signupForm.fname.$dirty&&signupForm.fname.$invalid" style="color:red">
<span ng-show="signupForm.fname.$error.required">First Name is required</span>
</span>
</p>
<p>
<b>Last Name:</b><br />
<input type="text" class="form-control" ng-required="true" ng-model="lname" name="lname"/>
<span ng-show="signupForm.lname.$dirty&&signupForm.lname.$invalid" style="color:red">
<span ng-show="signupForm.lname.$error.required">Last Name is required</span>
</span>
</p>
<p>
<b>Email:</b><br />
<input type="email" class="form-control" ng-required="true" name="email" ng-model="email"/>
<span ng-show="signupForm.email.$dirty&&signupForm.email.$invalid" style="color:red">
<span ng-show="signupForm.email.$error.required">Email is required</span>
<span ng-show="signupForm.email.$error.email"></span>
</span>
</p>
<p>
<b>Age:</b><br />
<input type="number" class="form-control" ng-required="true" ng-model="age"/>
<span ng-show="signupForm.age.$dirty&&signupForm.age.$invalid" style="color:red">
<span ng-show="signupForm.age.$error.required">Age is required</span>
<span ng-show="signupForm.age.$error.max ||signupForm.age.$error.min ">Age must be in between 18- 60 years</span>
</span>
</p>
<p>
<b>Password:</b><br />
<input type="password" class="form-control" ng-required="true" ng-model="password" name="password" ng-maxlength="16" ng-minlength="8"/>
<span ng-show="signupForm.password.$dirty&&signupForm.password.$invalid" style="color:red">
<span ng-show="signupForm.password.$error.required">Passowrd is required</span>
<span ng-show="signupForm.password.$error.maxlength ||signupForm.password.$error.minlength "></span>
</span>
</p>
<p>
<b>Confirm Password:</b><br />
<input type="password" class="form-control" ng-required="true" ng-model="conPassword" name="conPassword" ng-maxlength="16" ng-minlength="8" ng-equal="password"/>
<span ng-show="signupForm.conPassword.$dirty&&signupForm.conPassword.$invalid" style="color:red">
<span ng-show="signupForm.conPassword.$error.required">Passowrd is required</span>
<span ng-show="signupForm.conPassword.$error.maxlength ||signupForm.conPassword.$error.minlength "></span>
<span ng-show="signupForm.conPassword.$error.equal">Password do not match</span>
</span>
</p>
<p>
<b>Upload Profile Picture:</b><br />
<input type="file" value="Upload" /><input type="submit" value="Upload" ng-click="Upload()" ng-required="true"/>
</p>
<input type="submit" value="Submit" class="btn btn-default" ng-disabled="signupForm.$invalid"/>
</form>
I am Loading Partial View inside in ng-view in Index page but angular validation is not working inside the ng-view partial view . Please help anyone?

Related

AngularJS validation Error after Submitting and data clearing

I have implemented validation system in forms and it working fine.
Here is my form:
<form name='addContactForm'>
<div class="form-group">
<label for="userid">USER ID :</label><br>
<input ng-model="formModel.userid" class="form-control" name="userid" restrict-input="{type: 'digitsOnly'}" required>
<span style="color:red" ng-show="addContactForm.userid.$dirty && addContactForm.userid.$invalid">
<span ng-show="addContactForm.userid.$error.required"> User is required.</span>
<span ng-show="addContactForm.userid.$error.number">Invalid userID</span>
</div>
<div class="form-group">
<label for="name">NAME :</label><br>
<input ng-model="formModel.name" class="form-control" name="name" required/>
<span style="color:red" ng-show="addContactForm.name.$dirty && addContactForm.name.$invalid">
<span ng-show="addContactForm.name.$error.required"> Name is required.</span>
<span ng-show="addContactForm.name.$error.lettersOnly">Invalid Name</span>
</span>
</div>
<div class="form-group">
<label for="email">Email :</label> <br>
<input type="email" name="email" class="form-control" ng-model="formModel.email" ng-pattern="/[\w\d\.\_]+\#[\w\d]+\.[\w]{3}/" required>
<span style="color:red" ng-show="addContactForm.email.$dirty && addContactForm.email.$invalid">
<span ng-show="addContactForm.email.$error.required">Email is required.</span>
<span ng-show="addContactForm.email.$error.pattern">Invalid email address.</span>
</span>
</div>
<div class="form-group">
<label for="phone">Phone :</label> <br>
<input ng-model="formModel.phone" class="form-control" name="phone" restrict-input="{type: 'digitsOnly'}" required>
<span style="color:red" ng-show="addContactForm.phone.$dirty && addContactForm.phone.$invalid">
<span ng-show="addContactForm.phone.$error.required"> Phone Number is required.</span>
</span>
</div>
<div class="row justify-content-center">
<button class="btn btn-primary" ng-disabled="addContactForm.$invalid" ng-click="PassDataToDisplyThroughUrl()">Submit</button> <br>
</div>
</form>
</div>
Note that, above things are working fine.
my controller is below:
app.controller('formCtrl', ['$scope', '$location', '$http',
function($scope, $location, $http) {
$scope.formModel = {};
$scope.PassDataToDisplyThroughUrl = function() {
var url = 'display/' + $scope.formModel.userid + '/' + $scope.formModel.name
+ '/' + $scope.formModel.email + '/' + $scope.formModel.phone;
$location.path(url);
$http.post('http://127.0.0.1:8000/api/v1/contact/', $scope.formModel)
.then(function(response){
$scope.successCallBack = 'You have successfully saved your contact';
}, function(response){
$scope.errorCallBack = 'Opps! Unable to save your data, please check your network';
});
$scope.formModel = {};
$scope.addContactForm.$setPristine();
};
}]);
After i clicking on submit button -- it clear the form and send to the server. but the problem is, it shows me the validation error later
Can anyone fix me this issue?

angularjs form data is not post properly.?

My HTML form data is not POST
HTML View Page:-
<div class="col-sm-12">
<div class="card-box">
<form class="form-inline" name="addstock"
ng-submit="saveStockPurchaseInvoice()">
<h3 class="m-t-0 m-b-30 header-title text-center">Invoice
Details</h3>
<div class="row" style="margin-bottom: 20px;">
<div class="form-group col-md-3" style="">
<select name="supplier" ng-model="productStock.suppliername"
class="form-control" required="">
<option value="">Select a Supplier</option>
<option ng-repeat="allsupplier in supplier"
value="{{allsupplier.supplier_id}}">
{{allsupplier.supplier_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.supplier.$touched && addstock.supplier.$invalid">Supplier
is Required.</span>
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control"
ng-model="productStock.invoice" id="invoice" name="invoice"
placeholder="Enter Invoice Number" required> <br> <span
style="color: red"
ng-show="addstock.invoice.$touched && addstock.invoice.$invalid">Enter
Invoice Number.</span>
</div>
<div class="form-group col-md-3">
<input type="date" name="productdate" id="productdate"
ng-model="productStock.productdate" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productdate.$touched && addstock.productdate.$invalid">Date
is Required.</span>
</div>
</div>
<h3 class="m-t-0 m-b-30 header-title text-center">ITEMS</h3>
<div ng-repeat="productStock in productStocks"
style="margin-bottom: 10px;">
<div class="form-group">
<!--<input type="text" name="product_name" ng-model="productsStock.product_name" required="" placeholder="Product Name" class="form-control" id="product_name">-->
<select name="productStock_name"
ng-model="productStock.productStock" class="form-control"
required="">
<option value="">Select a Product</option>
<option ng-repeat="products in allproducts"
value="{{products.product_id}}">
{{products.product_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.productStock_name.$touched && addstock.productStock_name.$invalid">Product
Name Required.</span>
</div>
<div class="form-group">
<input id="productstock_qty" type="number" min="1" required=""
name="productstock_qty" ng-model="productStock.productstock_qty"
placeholder="Product Quantity" class="form-control"> <br>
<span style="color: red"
ng-show="addstock.productstock_qty.$touched && addstock.productstock_qty.$invalid">Product
Quantity required.</span>
</div>
<div class="form-group">
<input id="productstock_price" type="number" min="1"
ng-model="productStock.productstock_price"
name="productstock_price" placeholder="Product Price"
class="form-control" maxlength="15" size="10" required="">
<br> <span style="color: red"
ng-show="addstock.productstock_price.$touched && addstock.productstock_price.$invalid">Price
required.</span>
</div>
<div class="form-group">
<select name="product_units"
ng-model="productStock.productstock_units" class="form-control"
required="">
<option value="">Select Units</option>
<option ng-repeat="units in allunits" value="{{units.unit_id}}">
{{units.unit_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.product_units.$touched && addstock.product_units.$invalid">Units
is required.</span>
</div>
<div class="form-group">
<input id="productstock_cgst" type="text"
ng-model="productStock.productstock_cgst" name="productstock_cgst"
placeholder="CGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_cgst.$touched && addstock.productstock_cgst.$invalid">required.</span>
</div>
<div class="form-group">
<input id="productstock_sgst" type="text"
ng-model="productStock.productstock_sgst" name="productstock_sgst"
placeholder="SGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_sgst.$touched && addstock.productstock_sgst.$invalid">required.</span>
</div>
<!--<div class="form-group">-->
<!-- <input id="productstock_gst" type="text" ng-model="productStock.productstock_gst" ng-keydown="keydownevt()" name="productstock_gst" placeholder="GST" class="form-control" maxlength="10" size="6" required="">-->
<!-- <br>-->
<!-- <span style="color:red" ng-show="addstock.productstock_gst.$touched && addstock.productstock_gst.$invalid">required.</span> -->
<!--</div>-->
<div class="form-group">
<input id="productstock_total" type="text"
ng-model="productStock.productstock_total"
name="productstock_total" placeholder="Total" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_total.$touched && addstock.productstock_total.$invalid">Total
is required</span>
</div>
<button class="btn btn-danger btn-sm" ng-show="$last"
ng-click="removeStock()">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
<!-- ng-repeat close for add dynamic product field-->
<div class="form-group text-right m-b-0">
<button class="addfields btn btn-info waves-effect waves-light"
ng-click="addstockproduct()">Add Stock</button>
</div>
<div class="form-group text-right m-b-0 pull-right"
style="margin-top: 10px;">
<button type="submit" ng-click="" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-warning">Reset</button>
</div>
</form>
</div>
angularjs Controller:-
//Add Dynamic Fields.
$scope.productStocks = [{id: 'firstField2'}];
$scope.addstockproduct = function(){
var newItemNo2 = $scope.productStocks.length+1;
$scope.productStocks.push({'id':'field'+newItemNo2});
}
$scope.removeStock = function() {
var itemLast2 = $scope.productStocks.length-1;
$scope.productStocks.splice(itemLast2);
};
angularjs code for add records:-
$scope.productStock={};
$scope.suplier_succ = false;
$scope.suplier_err = false;
$scope.saveStockPurchaseInvoice=function(){
$http({
method: 'post',
url: 'stock/insert_stock',
data: $scope.productStock,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data)
{
if (data == 1) {
$scope.suplier_succ = $scope.suplier_succ ? false : true;
$scope.succ = "Stock added successfully";
$timeout(function () {
$(".modal").modal("hide");
}, 3000);
// $scope.formsup = {}; // clears input fields
// $scope.addsuppler.$setPristine();
// $scope.addsuppler.$setUntouched();
} else{
$scope.suplier_err = $scope.suplier_err ? false : true;
$scope.err = "Stock insertion failed! Try again.";
}
});
};
Codeigniter Controller:-
public function insert_stock(){
$request = json_decode(file_get_contents('php://input'), TRUE);
print_r($request);
}
enter image description here
My Result:-(All Records value is not POST)
enter image description here
I had the same problem with form data post.
I was able to resole it by setting headers: { 'Content-Type': undefined } and transformRequest: angular.identity in http request.
below is my sample code.
function Upload(containerName, objFile, objParams) {
var deferred = $q.defer();
var frmData = new FormData();
frmData.append('file', objFile);
var req = {
method: 'POST',
url: window.APIBaseUrl + 'containers/' + containerName + '/upload',
data: frmData,
params: objParams,
headers: { 'Content-Type': undefined },
transformRequest: angular.identity,
dataType: "json"
}
$http(req).then(function (response) {
deferred.resolve(response.data);
}, function (err) {
deferred.reject(handleHttpError(err));
$log.error("Error in upload", err);
});
return deferred.promise;
}
This May help you
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div class="col-sm-6" style="width:50%;">
<div class="card-box">
<form class="form-inline" name="addstock"
ng-submit="saveStockPurchaseInvoice()">
<h3 class="m-t-0 m-b-30 header-title text-center">Invoice
Details</h3>
<div class="row" style="margin-bottom: 20px;">
<div class="form-group col-md-3" style="">
<select name="supplier" ng-model="productStock.suppliername"
class="form-control" required="">
<option value="">Select a Supplier</option>
<option ng-repeat="allsupplier in supplier"
value="{{allsupplier.supplier_id}}">
{{allsupplier.supplier_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.supplier.$touched && addstock.supplier.$invalid">Supplier
is Required.</span>
</div>
<div class="form-group col-md-3">
<input type="text" class="form-control"
ng-model="productStock.invoice" id="invoice" name="invoice"
placeholder="Enter Invoice Number" required> <br> <span
style="color: red"
ng-show="addstock.invoice.$touched && addstock.invoice.$invalid">Enter
Invoice Number.</span>
</div>
<div class="form-group col-md-3">
<input type="date" name="productdate" id="productdate"
ng-model="productStock.productdate" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productdate.$touched && addstock.productdate.$invalid">Date
is Required.</span>
</div>
</div>
<h3 class="m-t-0 m-b-30 header-title text-center">ITEMS</h3>
<div ng-repeat="item in productStocks track by $index"
style="margin-bottom: 10px;">
<div class="form-group">
<!--<input type="text" name="product_name" ng-model="productsStock.product_name" required="" placeholder="Product Name" class="form-control" id="product_name">-->
<select name="productStock_name"
ng-model="productStock.items[$index].productStock" class="form-control"
required="">
<option value="">Select a Product</option>
<option ng-repeat="products in allproducts"
value="{{products.product_id}}">
{{products.product_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.productStock_name.$touched && addstock.productStock_name.$invalid">Product
Name Required.</span>
</div>
<div class="form-group">
<input id="productstock_qty" type="number" min="1" required=""
name="productstock_qty" ng-model="productStock.items[$index].productstock_qty"
placeholder="Product Quantity" class="form-control"> <br>
<span style="color: red"
ng-show="addstock.productstock_qty.$touched && addstock.productstock_qty.$invalid">Product
Quantity required.</span>
</div>
<div class="form-group">
<input id="productstock_price" type="number" min="1"
ng-model="productStock.items[$index].productstock_price"
name="productstock_price" placeholder="Product Price"
class="form-control" maxlength="15" size="10" required="">
<br> <span style="color: red"
ng-show="addstock.productstock_price.$touched && addstock.productstock_price.$invalid">Price
required.</span>
</div>
<div class="form-group">
<select name="product_units"
ng-model="productStock.items[$index].productstock_units" class="form-control"
required="">
<option value="">Select Units</option>
<option ng-repeat="units in allunits" value="{{units.unit_id}}">
{{units.unit_name}}</option>
</select> <br> <span style="color: red"
ng-show="addstock.product_units.$touched && addstock.product_units.$invalid">Units
is required.</span>
</div>
<div class="form-group">
<input id="productstock_cgst" type="text"
ng-model="productStock.items[$index].productstock_cgst" name="productstock_cgst"
placeholder="CGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_cgst.$touched && addstock.productstock_cgst.$invalid">required.</span>
</div>
<div class="form-group">
<input id="productstock_sgst" type="text"
ng-model="productStock.items[$index].productstock_sgst" name="productstock_sgst"
placeholder="SGST" class="form-control" maxlength="10" size="6"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_sgst.$touched && addstock.productstock_sgst.$invalid">required.</span>
</div>
<!--<div class="form-group">-->
<!-- <input id="productstock_gst" type="text" ng-model="productStock.productstock_gst" ng-keydown="keydownevt()" name="productstock_gst" placeholder="GST" class="form-control" maxlength="10" size="6" required="">-->
<!-- <br>-->
<!-- <span style="color:red" ng-show="addstock.productstock_gst.$touched && addstock.productstock_gst.$invalid">required.</span> -->
<!--</div>-->
<div class="form-group">
<input id="productstock_total" type="text"
ng-model="productStock.items[$index].productstock_total"
name="productstock_total" placeholder="Total" class="form-control"
required=""> <br> <span style="color: red"
ng-show="addstock.productstock_total.$touched && addstock.productstock_total.$invalid">Total
is required</span>
</div>
<button class="btn btn-danger btn-sm" ng-show="$last"
ng-click="removeStock()">
<span class="glyphicon glyphicon-remove"></span>
</button>
</div>
<!-- ng-repeat close for add dynamic product field-->
<div class="form-group text-right m-b-0">
<button class="addfields btn btn-info waves-effect waves-light"
ng-click="addstockproduct()">Add Stock</button>
</div>
<div class="form-group text-right m-b-0 pull-right"
style="margin-top: 10px;">
<button type="submit" ng-click="" class="btn btn-primary">Save</button>
<button type="reset" class="btn btn-warning">Reset</button>
</div>
</form>
</div>
</div>
<div style="width:50%">
<pre>{{productStock | json}}</pre>
</div>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope,$http) {
$scope.productStocks = [{id: 'firstField2'}];
$scope.supplier = [{supplier_id:1, supplier_name:'abc'}];
$scope.allproducts = [{ product_name:'qwerty',product_id:1 }];
$scope.allunits = [{ unit_id:1,unit_name:'iop'}];
$scope.addstockproduct = function(){
var newItemNo2 = $scope.productStocks.length+1;
$scope.productStocks.push({'id':'field'+newItemNo2});
}
$scope.removeStock = function() {
var itemLast2 = $scope.productStocks.length-1;
$scope.productStocks.splice(itemLast2);
};
$scope.productStock={};
$scope.suplier_succ = false;
$scope.suplier_err = false;
$scope.saveStockPurchaseInvoice=function(){
$http({
method: 'POST',
url: 'http://abcd/insert', //change to your url
data: JSON.stringify($scope.productStock),
//headers: {'Content-Type': 'application/x-www-form-urlencoded'}
headers: { 'Content-Type': undefined },
transformRequest: angular.identity,
dataType: "json"
// 'Content-Type':'application/json'
}).then(function (response)
{
if (data == 1) {
$scope.suplier_succ = $scope.suplier_succ ? false : true;
$scope.succ = "Stock added successfully";
$timeout(function () {
$(".modal").modal("hide");
}, 3000);
// $scope.formsup = {}; // clears input fields
// $scope.addsuppler.$setPristine();
// $scope.addsuppler.$setUntouched();
} else{
$scope.suplier_err = $scope.suplier_err ? false : true;
$scope.err = "Stock insertion failed! Try again.";
}
});
};
//$request = json_decode(file_get_contents('php://input'), TRUE);
// print_r($request);
});
</script>
</body>
</html>

Angular object not binding

I have the following issue. I have 2 controllers defined for 2 separate functions.
The 1st controller handle my login and the second 1 handles forgot password.
Login controller binds well but forgot password controller does bind but not the object.
Controllers
var singlePageApp = angular.module('SinglePageApp',[]);
singlePageApp.controller('LoginController', function ($scope, $location, $window) {
$scope.isFormValid = false;
$scope.message = null;
$scope.login = null;
// Login object
$scope.Login = {
Email: '',
Password: '',
};
//check form validation
$scope.$watch('LoginForm.$valid', function (newValue) {
$scope.isFormValid = newValue;
});
$scope.submitForm = function () {
console.log('Form is submitted with:', $scope.login);
...
};
});
singlePageApp.controller('ForgotPasswordController', function ($scope, $location, $window) {
$scope.message = null;
$scope.password = null;
// Password object
$scope.Password = {
Email: '',
};
$scope.$watchGroup(['password', 'Password'], function (newValues, oldValues, scope) {
console.log(newValues);
});
$scope.forgotPassword = function () {
};
});
Login HTML
<div ng-controller="LoginController">
<form class="form-signin mt10" name="LoginForm">
<h2 class="form-signin-heading pwc">Please login</h2>
<div class="login-wrap">
<input ng-model="login.Email" id="tbEmail" name="Email" type="email" class="form-control" placeholder="Email" autofocus required>
<span class="error" ng-show="LoginForm.Email.$touched && LoginForm.Email.$error.required">Email is required</span>
<input ng-model="login.Password" id="tbPassword" name="Password" type="password" class="form-control" placeholder="Password" required>
<span class="error" ng-show="LoginForm.Password.$touched && LoginForm.Password.$error.required">Password is required</span>
<label class="checkbox">
<span class="pull-right">
#* Forgot Password?*#
Forgot Password?
</span>
</label>
<div class="clearfix"></div>
<button id="btnloginAdmin" type="button" ng-click="submitForm()" ng-disabled="LoginForm.$invalid && !!LoginForm.$error.email" class="btn btn-success btn-block">Login</button>
<div class="text-center">
<label style="color: red;" id="lblError" class="hasError-label clearfix">{{message}}</label>
<div class="error error-red"></div>
</div>
</div>
</form>
</div>
HTML Modal
<div ng-controller="ForgotPasswordController">
<div aria-hidden="false" aria-labelledby="myModalLabel" role="dialog" tabindex="-1" id="modalForgotPassword" class="modal fade">
<div class="modal-dialog">
<div class="modal-content" ng-form="fgForm">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Forgot Password ?</h4>
</div>
<div class="modal-body">
<p>Enter your e-mail address below to reset your password.</p>
<input type="email" name="Email" ng-model="password.Email"
placeholder="Email" autocomplete="off"
class="form-control placeholder-no-fix" required>
<span class="error" ng-show="fgForm.Email.$touched && fgForm.Email.$error.required">Email is required</span>
<div class="text-center">
<label style="color: red;" id="fError" class="hasError-label clearfix">{{message}}</label>
<div class="error error-red"></div>
</div>
</div>
<div class="modal-footer">
<a href="javascript:;" class="btn btn-success" ng-disabled="fgForm.$invalid && !fgForm.$error.email" ng-click="forgotPassword();">
<i class="fa fa-check"></i> | Submit
</a>
<a href="javascript:;" class="btn btn-danger" data-dismiss="modal">
<i class="fa fa-times"></i> | Cancel
</a>
</div>
</div>
</div>
</div>
I have tried changing to bind to a single object 'password' but it states its undefined. Then I changed it to 'Password.Email' but then it stays empty.
The 2 controllers are bind on the same page so not sure if that is the issue.
Thank you in advance.

login validation and credentials using angularjs

I am new to angularjs. I have created simple login page. The code is working fine, but my validation and credentials have some problem. I found some clues in few sites, but those answers din't provide the proper information for my solution. The error that I am facing is in the validation. When I enter the invalid mailid and password it should pop-up error message . But I could not achieve it.
HTML :
`<form ng-submit="loginform()" class="ng-scope ng-pristine ng-valid center" name="logform"><br/><br>`
<div class="form-group col-md-4">
<label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you#example.com" ng-model="username" ng-pattern="emailFormat">
<span ng-show="logform.uname.$dirty.username && loginform.uname.$invalid"></span>
<span style="color: Red" ng-show="logform.uname.$valid">* Email is required</span>
<span style="color: Red" ng-show="logform.uname.$error.username">* Invalid ID </span>
</div>
<div class="form-group col-md-4">
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password">
<span style="color: red" ng-show="logform.pwd.$dirty && loginform.pwd.$invalid"></span>
<span ng-show="logform.pwd.$error.$valid">* Password is required</span>
<span ng-show="logform.pwd.$error.pwd">* Invalid Password</span>
</div>
<div class="col-md-4">
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="submit()" ng-Disabled="logform.username.$dirty && loginform.username.$invalid || logform.pwd.$dirty && loginform.pwd.$invalid">Login</button>
</div>
AngularJs :
`var app = angular.module('logapp',['toastr']);`
app.factory('authentication', function() {
return {
isAuthenticated: false,
user: null
}
});
app.controller('credientials', function($scope,toastr,authentication) {
$scope.loginform = function (username, password) {
if ($scope.username === 'admin#evol.com' && $scope.password === '123') {
authentication.isAuthenticated = true;
$location.path("/home");
} else {
toastr.error('Invalid username and password');
}
};
});
Kindly read the snippet below and alter your code to setValidity() based on condition.
var app = angular.module('logapp', []);
app.factory('authentication', function() {
return {
isAuthenticated: false,
user: null
}
});
app.controller('credientials', function($scope, authentication) {
$scope.loginform = function(isValid) {
$scope.logform.uname.$setValidity("username", true);
$scope.logform.pwd.$setValidity("pwd", true);
if (isValid) {
if ($scope.username == 'admin#evol.com' && $scope.password == '123') {
alert("success");
authentication.isAuthenticated = true;
} else {
$scope.logform.uname.$setValidity("username", false);
$scope.logform.pwd.$setValidity("pwd", false);
}
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body ng-app="logapp" ng-controller="credientials">
<form ng-submit="loginform(logform.$valid)" class="ng-scope ng-pristine ng-valid center" name="logform" novalidate role="form">
<div class="form-group col-md-4">
<label form="emailinput"><b>Email</b></label>
<input type="email" class="form-control" name="uname" id="emailinput" placeholder="you#example.com" ng-model="username" ng-pattern="emailFormat" required>
<span style="color: Red" ng-show="logform.$submitted && logform.uname.$error.required">* Email is required</span>
<span style="color: Red" ng-show="logform.$submitted && logform.uname.$error.username">* Invalid ID </span>
</div>
<div class="form-group col-md-4">
<label form="pwdinput"><b>Password</b></label>
<input type="password" class="form-control" name="pwd" id="pwdinput" placeholder="*******" ng-model="password" required>
<span ng-show="logform.$submitted && logform.pwd.$error.required">* Password is required</span>
<span ng-show="logform.$submitted && logform.pwd.$error.pwd">* Invalid Password</span>
</div>
<div class="col-md-4">
<button type="cancel" class="btn" ng-click="toggle_cancel()">Cancel</button>
<button class="btn btn-primary" type="submit">Login</button>
</div>
</form>
</body>

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