I have tried this code..
<script>
angular.module('emailExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.text = 'me#example.com';
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
Email: <input type="email" name="input" ng-model="text" required>
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.email">
Not valid email!</span>
<tt>text = {{text}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
<tt>myForm.$error.email = {{!!myForm.$error.email}}</tt><br/>
</form>
But getting issue in validation. Say if i try to enter 'a#a.c' in email then it will not show any error. Please help me on this.
I am thinking why you are not using ng-pattern for this. You can make a regular expression and put inside ng-pattern.
Example:
<form name="signupFrm">
<input type="email", ng-pattern="emailPattern">
</form>
or,
HTML:
<div ng-class="{'has-error': signupFrm.email.$dirty && signupFrm.email.$invalid}">
<input type="email" placeholder="Email" name="email" ng-pattern="emailPattern" required="required">
<div ng-show="signupFrm.email.$dirty && signupFrm.email.$invalid || signupFrm.email.$error.pattern">
<span ng-show="signupFrm.email.$error.pattern && signupFrm.email.$invalid " class="help-block"> should look like an email address</span>
<span ng-show=" signupFrm.email.$error.required" class="help-block">can't be blank</span>
</div>
</div>
JS:
$scope.emailPattern = /^([a-zA-Z0-9])+([a-zA-Z0-9._%+-])+#([a-zA-Z0-9_.-])+\.(([a-zA-Z]){2,6})$/;
Related
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?
this is my input
aby-saturn#gmail.com
hyphen is not working in emailid getting invalid email id
this is my ng-pattern
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input class="form-control" type="email" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="/^[_a-z0-9]+(\.[_a-z0-9]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/" noncapitalize required />
</div>
<div style="color: red" id="emailError"></div>
<span style="color:red;" class="email-error" ng-show="loginForm.submitted && loginForm.email.$error.required">Required</span>
<span style="color:red" class="error" ng-show="loginForm.submitted && loginForm.email.$error.pattern">Email not valid</span>
</div>
Here is a regexp for email validation.
^[\w-]+(\.[\w-]+)*#([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$
You can test this regexp here
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="">
<p>Try writing in the input field:</p>
<form name="myForm">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input class="form-control" type="text" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="/^[\w-]+(\.[\w-]+)*#([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/" noncapitalize required />
</div>
<div style="color: red" id="emailError"></div>
<span style="color:red;" class="email-error" ng-show="myForm.email.$error.required">Required</span>
<span style="color:red" class="error" ng-show="myForm.email.$error.pattern">Email not valid</span>
</div>
</form>
</body>
</html>
PLEASE RUN THE ABOVE SNIPPET
Here is a working DEMO
I guess you don't want to allow '-' to be in the email address, here is what I got using ng-pattern and if you want '-' to be allowed check 2nd example:
1) If '-' is not allowed:
angular.module('myApp', [])
.controller('MyController', function($scope) {
$scope.user = {
email: 'test#test.com'
};
$scope.emailPattern = /^(([^-<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
<form name="loginForm">
<div class="form-group">
<div class="input-group">
<input class="form-control" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="emailPattern" noncapitalize required />
</div>
<div style="color: red" id="emailError">
<span style="color:red;" class="email-error" ng-show="loginForm.email.$error.required">Required</span>
<span style="color:red" class="error" ng-show="loginForm.email.$error.pattern">Email not valid, doesn't match the provided pattern</span>
</div>
</div>
</form>
</div>
2) If '-' is allowed:
angular.module('myApp', [])
.controller('MyController', function($scope) {
$scope.user = {
email: 'test#test.com'
};
$scope.emailPattern = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
<form name="loginForm">
<div class="form-group">
<div class="input-group">
<input class="form-control" id="email" name="email" placeholder="Enter Email" ng-model="user.email" ng-pattern="emailPattern" noncapitalize required />
</div>
<div style="color: red" id="emailError">
<span style="color:red;" class="email-error" ng-show="loginForm.email.$error.required">Required</span>
<span style="color:red" class="error" ng-show="loginForm.email.$error.pattern">Email not valid, doesn't match the provided pattern</span>
</div>
</div>
</form>
</div>
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>
I don't seem to understand why ng-show is failing to display this validation when the passwords don't match. What am I missing?
<form name="myForm">
<div class="form-group">
<input ng-required="true" name="username" ng-minlength="3" ng-maxlength="10" ng-model="model.user.username" type="text" class="form-control" id="user-name" placeholder="Username">
<p class="errorValidationText" ng-show="myForm.username.$invalid && myForm.username.$touched">Please pick a username between 3 - 10 characters.</p>
</div>
<div class="form-group">
<input ng-required="true" name="password" ng-minlength="4" ng-model="model.user.password" type="password" class="form-control" id="password" placeholder="Password">
<p class="errorValidationText" ng-show="myForm.password.$invalid && myForm.password.$touched">Password should contain atleast 4 characters.</p>
</div>
<div class="form-group">
<input ng-required="true" name="verifyPassword" ng-minlength="4" ng-model="model.user.verifyPassword" type="password" class="form-control" id="verify-password" placeholder="Verify Password">
<p class="errorValidationText" ng-show="(myForm.verifyPassword.value != '') && (myForm.password.value != myForm.verifypassword.value)">Passwords don't match.</p>
</div>
<button ng-disabled="myForm.username.$invalid || myForm.password.$invalid || myForm.verifyPassword" ng-click="model.register(model.user)" class="btn btn-success btn-block">Register</button>
</form>
Thanks!!
I've reffered the other solutions, none of the existing questions are similar to what I am doing.
The myForm object doesn't have the input values, you can see that with {{myForm}}. Use the value from ngModel instead:
<p ng-show="(user.model.verifyPassword != '') && (user.model.password != user.model.verifyPassword)">Passwords don't match</p>
Also notice that if your input is not valid, ngModel won't set the value: user.model.verifyPassword != '' is going to be always true, since you set ng-minlength=4.
Another tip: ng attributes make sense only if you evaluate a scope variable, ng-required="true" is the same as required="required".
myForm.verifyPassword.value casing is different in your statement myForm.verifypassword.value
I made up an example as I didn't have your code
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.myForm = {
password: '',
verifyPassword: ''
};
$scope.submit = function() {
console.log('success');
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
Password:
<input type="password" ng-model="myForm.password">
<br>Verify Password:
<input type="password" ng-model="myForm.verifyPassword">
<br>
<p ng-show="(myForm.verifyPassword != '') && (myForm.password != myForm.verifyPassword)">Passwords don't match</p>
<button>Submit</button>
</div>
I have a couple of fields that I would like to replicate dynamically. I'm using ng-reapt which is doing the job. However, the validation messages are not working. Here's what I've got:
<html ng-app="app">
<head>
<title>teste</title>
</head>
<body ng-controller='testController'>
<label>Number of Workers</label>
<select ng-model="quantity" ng-change="changed()">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<form name='frm' action='/workers/add' method='POST'>
<div ng-repeat="i in numberOfWorkers track by $index">{{$index + 1}}
<div>
<label>First Name</label>
<input class="fullSize" letters-only placeholder="Please enter your name" type="text" name="firstName" ng-model="workers[$index].firstName" ng-minlength="3" ng-maxlength="50" ng-required="true" maxlength="50">
<span ng-cloak class="error-container" ng-show="submitted || showErrors(frm.firstName)">
<small class="error" ng-show="frm.firstName[{{$index}}].$error.required">* Please enter your name.</small>
<small class="error" ng-show="frm.firstName[{{$index}}].$error.minlength">* At least 3 chars.</small>
<small class="error" ng-show="frm.firstName[{{$index}}].$error.maxlength">* No more than 50 chars.</small>
</span>
</div>
<div>
<label>Surname</label>
<input class="fullSize" letters-only placeholder="Please enter your surname" type="text" name="surName" ng-model="workers[$index].surName" ng-minlength="3" ng-maxlength="50" ng-required="true" maxlength="50">
<span ng-cloak class="error-container" ng-show="submitted || showErrors(frm.surName)">
<small class="error" ng-show="frm.surName[$index].$error.required">* Please enter your name.</small>
<small class="error" ng-show="frm.surName[$index].$error.minlength">* At least 3 chars.</small>
<small class="error" ng-show="frm.surName[$index].$error.maxlength">* No more than 50 chars.</small>
</span>
</div>
<div>
<label>Email</label>
<input class="grid-full" placeholder="Please enter your e-mail" type="email" name="email" ng-model="workers[$index].email" ng-minlength="3" ng-maxlength="50" required maxlength="50">
<span class="error-container" ng-show="submitted || showErrors(frm.email)">
<small class="error" ng-show="frm.email[$index].$error.required">* Please enter your E-mail.</small>
<small class="error" ng-show="frm.email[$index].$error.email">* Invalid email.</small>
</span>
</div>
</div>
</form>
<button ng-click="test()">test</button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script>
var app = angular.module("app",[]);
app.controller('testController',['$scope', function($scope){
$scope.quantity = 1;
$scope.submitted = false;
$scope.numberOfWorkers = [1];
$scope.workers = [];
$scope.getNumber = function (num) {
return new Array(num);
};
$scope.test = function(){
console.log($scope.workers);
};
$scope.changed = function() {
$scope.workers = [];
$scope.numberOfWorkers = new Array(parseInt($scope.quantity));
}
$scope.isUndefined = function (thing) {
var thingIsUndefined = (typeof thing === "undefined");
return thingIsUndefined;
};
$scope.showErrors = function (field) {
var fieldIsUndefined = $scope.isUndefined(field);
if (fieldIsUndefined == false)
{
var stateInvalidIsUndefined = $scope.isUndefined(field.$invalid);
var stateDirtyIsUndefined = $scope.isUndefined(field.$dirty);
return (fieldIsUndefined == false && stateInvalidIsUndefined == false && stateDirtyIsUndefined == false &&
(field.$invalid && field.$dirty));
}
return false;
};
}]);
</script>
</body>
</html>
Is there a way to display and validate fields using the index position?
frm.firstName.$error.minlength
The code above works for the first block, but it display the message to all copies.
I think this along the lines of what you're trying to achieve:
http://plnkr.co/edit/HVwnRfvt30WrsteY6DvW
We need to assign a dynamic name to each input field, by way of using the $index of the ngRepeat:
<form name="form">
<div ng-repeat="entry in array track by $index">
<input type="text" name="someField{{$index}}">
<div>
</form>
And based on the number of entries in array, we can access in a loop each dynamically created someField using the syntax below:
form['someField' + $index]
In your case, let's say we have three workers, and the second worker's firstName is invalid -- in an ngRepeat, it would look like this:
<div ng-repeat="i in numberOfWorkers track by $index">
isValid? --> form.firstName{{$index}}.$valid = {{form['firstName' + $index].$valid}}
</div>
Output:
isValid? --> form.firstName0.$valid = true
isValid? --> form.firstName1.$valid = false
isValid? --> form.firstName2.$valid = true