AngularJS validation Error after Submitting and data clearing - angularjs

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?

Related

Form Validation not working AngularJS

I am trying to do form validation using AngularJS in Laravel Blade, but it isn't working and when clicked on the submit button undefined gets printed in the console.
HTML:
<div class="uk-grid" ng-app="validationApp" ng-controller="mainController">
<form name="signupForm" class="uk-form uk-width-1-2" novalidate>
{{ csrf_field() }}
<fieldset class="pad50">
<div class="uk-form-row">
<input class="bradius2" placeholder="Username" type="text" name="username" ng-modal="user.username" required>
<div ng-show="signupForm.$submitted || signupForm.username.$touched">
<span ng-show="signupForm.username.$error.required">Username is required.</span>
</div>
</div>
<div class="uk-form-row">
<input class="bradius2" placeholder="Email" type="email" name="user_mail" ng-modal="user.user_mail" required>
<div ng-show="signupForm.$submitted || signupForm.user_mail.$touched">
<span ng-show="signupForm.user_mail.$error.required">Email is required.</span>
<span ng-show="signupForm.user_mail.$error.email">Enter a valid email.</span>
</div>
</div>
<div class="uk-form-row">
<input class="bradius2" placeholder="Password" type="password" name="user_pass" ng-modal="user.user_pass" required>
<div ng-show="signupForm.$submitted || signupForm.user_pass.$touched">
<span ng-show="signupForm.user_pass.$error.required">Password is required</span>
</div>
</div>
<div class="uk-form-row">
<input class="bradius2" placeholder="Confirm Password" type="password" name="user_cpass" ng-modal="user.user_cpass" required>
<div ng-show="signupForm.$submitted || signupForm.user_cpass.$touched">
<span ng-show="signupForm.user_cpass.$error.required">Confirm the password</span>
</div>
</div>
<div class="uk-form-row">
<button class="uk-button bgorange butn uk-button-large" type="submit" id="sign_up_button" ng-click="signupUser(user)">
<b>SIGN UP VIA EMAIL</b>
</button>
</div>
</fieldset>
</form>
</div>
app.js:
angular.module('validationApp', [])
.controller('mainController', ['$scope', function($scope) {
$scope.master = {};
$scope.signupUser = function(user) {
$scope.master = angular.copy(user);
console.log(user);
};
}]);
I am using Angular version 1.6.1 and also UIKit.
Thanks in advance!
It has to be ng-model instead of ng-modal.
ng-model is the one which stores the user input in form.
undefined gets printed in the console
you need to initialize the user object
angular.module('validationApp', [])
.controller('mainController', ['$scope', function($scope) {
$scope.master = {};
$scope.user = {};// here
$scope.signupUser = function(user) {
$scope.master = angular.copy(user);
console.log(user);
};
}]);
Form Validation not working AngularJS
You should add this code on your form tag
ng-submit="mainForm.$valid && yourfunction()" //yourfunction means which event you want to be fire
notice ng-messages:
<div class="uk-form-row" >
<input class="bradius2" placeholder="Username" type="text" name="username" ng-model="user.username" required>
<div ng-messages="signupForm.username.$error" ng-show="signupForm.$submitted || signupForm.username.$touched">
<span ng-show="signupForm.username.$error.required">Username is required.</span>
</div>
</div>

how to error message on submitting of a from in angular js

I want to show the required error message once the from is submitted but not sure how to do it i am trying as shown below
<div class="controls">
<form name="formx" >
<ul class="form small">
<div class="tag">ID</div>
<input type="text" class="small" name="enrolmentId" ng-model="enrolmentDetail.Id" required="" onkeypress='return !(event.charCode == 32 )'>
<div class="error text-danger" style="position: absolute;margin-left: 120px;" ng-show="formx.enrolmentId.$error.required && (formx.$submitted || formx.enrolmentId.$touched)"><p>Id is required</p></div>
</li>
<li>
<div class="btn" ng-click="saveDetails(enrolmentDetail)" ng-show="formAction=='save'">SAVE</div>
</li>
</ul>
</form>
</div>
here now the error message is shown once we dirty the textbox but i want to show the error message once the form is submitted and if there is required error from should not be submitted
trying used ng-submit but not sure how to do it
Please help by creating a fiddle or posting a example to do it
This is an example
html
<body ng-app="myApp">
<div ng-controller="myCtrl as mc">
<form class="form" name="myForm" ng-submit="mc.submit(myForm)" novalidate>
<div class="form-group">
<label for="username">Username</label>
<input name="username" class="form-control" type="text" ng-model="mc.username" required/>
<p class="text-danger" ng-show="myForm.username.$error.required && mc.submitted">Username is required</p>
</div>
<div class="form-group">
<label for="password">Password</label>
<input name="password" class="form-control" type="password" ng-model="mc.password" required/>
<p class="text-danger" ng-show="myForm.password.$error.required && mc.submitted">Password is required</p>
</div>
<button class="btn btn-success">submit</button>
</form>
<p class="text-success" ng-show="mc.sent && mc.submitted">Form sent</p>
js
angular.module("myApp",[])
.controller("myCtrl", function(){
var vm = this;
vm.submit = submit;
function submit(form){
vm.submitted = true;
if(form.$valid && vm.submitted === true){
//Send data logic
vm.sent = true;
}
}

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

Angular form validation is not working

I have the following form :
UPDATE
<script type="text/ng-template" id="form-profile.html">
<form id="f1" name="form">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" ng-model="formData.name" ng-class="submitted ? 'ng-dirty' : ' ' " required autofocus>
<span style="color:red" ng-show="form.name.$dirty || form.name.$invalid">Name is required</span>
</div>
<div class="form-group">
<label for="name">E-mail</label>
<input type="text" class="form-control" name="email" ng-model="formData.email" ng-class="submitted ? 'ng-dirty' : ' '" required autofocus>
<span style="color:red" ng-show="f1.email.$dirty && f1.email.$invalid">
<span ng-show="f1.email.$error.required">Email is required.</span>
</span>
</div>
<div class="form-group">
<label for="Cellphone">Mobil nr.</label>
<input type="text" class="form-control" name="Cellphone" ng-model="formData.Cellphone" ng-class="submitted ? 'ng-dirty' : ' '" required autofocus>
<span style="color:red" ng-show="f1.Cellphone.$dirty && f1.Cellphone.$invalid">
<span ng-show="f1.Cellphone.$error.required">Cellphone is required.</span>
</span>
</div>
<div class="form-group">
<label for="address">Adresse</label>
<input type="text" class="form-control" name="address" ng-model="formData.address">
</div>
<div class="form-group col col-33 col-offset-67">
<a ui-sref="front.form.organisation" class="button icon-right ion-chevron-right button-energized .col-offset-50">
Next
</a>
</div>
</form>
</script>
When I press next I want the form to disable the next button and show me the errors.
I have tried with name so far and I did not get the errors.
Thank you
Please see demo below
ng-show="f1.name.$dirty <-- f1 that's form name not id
var app = angular.module('app', []);
app.controller('homeCtrl', function($scope) {
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="homeCtrl">
<form name="f1">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" ng-model="formData.name" required autofocus>
<span style="color:red" ng-show="f1.name.$dirty && f1.name.$invalid">Name is required</span>
</div>
</form>
</div>

angularJS show validation of the message

here is the angular code.
.controller("myStuList", ['$scope', "weejoinMessage", "httpServer", "$modal", "ngWeeJoinServer", "stuServer", function ($scope, weejoinMessage, httpServer, $modal, server, stuServer) {
//第一次进入是加载所有学员数据
$scope.stuinfos = infos.info; // i give the data form info.info to stuinfos.
//
$scope.course = '';
$scope.class = '';
$scope.name = '';
$scope.age = '';
$scope.scores = '';
$scope.createNew = function () {
httpServer.httpHandle({
url: stuBag.action.createNew,
data: {
courseId: $scope.course,
classId:$scope.class,
stuname: $scope.name,
age: $scope.age,
scores: $scope.scores
},
success: function (data) {
weejoinMessage.messageSuccess("添加成功!");
$scope.stuinfos.push({ 'CourseName': $scope.course, 'ClassName': $scope.class, 'StuName': $scope.name, 'Age': $scope.age, 'Scores': $scope.scores });
}
});
};
please help to see the problem. all the validation message show up togather when open the create page.
look down the html code .i want to show the validation message when click on the input text. and when the input message is wrong according to the validation . and show the message below the input texbox.
<div ng-app="stu.info" ng-controller="myStuList">
<form name="createnew_form" class="form-horizontal" role="form" ng-submit="createNew()" novalidate>
<div class="form-group">
<label for="stuname" class="col-md-2 control-label">姓名</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="姓名" name="stuname" ng-model="name" ng-minlength=3 ng-maxlength=20 required />
<div class="error" ng-show="createnew_form.stuname.$dirty && createnew_form.stuname.$invalid">
<small class="error" ng-show="createnew_form.stuname.$error.required">
请输入姓名
</small>
<small class="error" ng-show="createnew_form.stuname.$error.minlength">
至少输入两个字
</small>
<small class="error" ng-show="createnew_form.stuname.$error.maxlength">
最多输入五个字
</small>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">年龄</label>
<div class="col-md-4">
<input type="number" min="0" max="100" required class="form-control" name="age"
ng-model="age" />
<div class="error-container" ng-show="createnew_form.age.$dirty && createnew_form.age.$invalid">
<small class="error" ng-show="createnew_form.age.$error.required">
请输入年龄
</small>
<small class="error" ng-show="createnew_form.age.$error.min">
最小年龄为0
</small>
<small class="error" ng-show="createnew_form.age.$error.max">
最大年龄为100
</small>
</div>
</div>
</div>
<div class="form-group">
<div style="padding-left:110px">
<input type="submit" value="确认添加" class="btn btn-primary" />
</div>
</div>
</form>
</div>
I have used your code on jsFiddle and the form validation is working as expected.
I'm not seeing all the error messages together as you've mentioned. Please see the fiddle here: http://jsfiddle.net/giri_jeedigunta/4508cd4r/
Your HTML Code:
<div ng-app="stu.info" ng-controller="myStuList">
<form name="createnew_form" class="form-horizontal" role="form" ng-submit="createNew()" novalidate>
<div class="form-group">
<label for="stuname" class="col-md-2 control-label">姓名</label>
<div class="col-md-4">
<input type="text" class="form-control" placeholder="姓名" name="stuname" ng-model="name" ng-minlength=3 ng-maxlength=20 required />
<div class="error" ng-show="createnew_form.stuname.$dirty && createnew_form.stuname.$invalid">
<small class="error" ng-show="createnew_form.stuname.$error.required">
请输入姓名
</small>
<small class="error" ng-show="createnew_form.stuname.$error.minlength">
至少输入两个字
</small>
<small class="error" ng-show="createnew_form.stuname.$error.maxlength">
最多输入五个字
</small>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">年龄</label>
<div class="col-md-4">
<input type="number" min="0" max="100" required class="form-control" name="age"
ng-model="age" />
<div class="error-container" ng-show="createnew_form.age.$dirty && createnew_form.age.$invalid">
<small class="error" ng-show="createnew_form.age.$error.required">
请输入年龄
</small>
<small class="error" ng-show="createnew_form.age.$error.min">
最小年龄为0
</small>
<small class="error" ng-show="createnew_form.age.$error.max">
最大年龄为100
</small>
</div>
</div>
</div>
<div class="form-group">
<div style="padding-left:110px">
<input type="submit" value="确认添加" class="btn btn-primary" />
</div>
</div>
</form>
</div>
Shouldn't creatnew_form.course.$invalid be createnew_form.course.$invalid (with an 'e' in "createnew")?

Resources