validation on keyup is working but on click of submit button it's not working. When i click on submit it does no validate although redirect to some other page.
<script>
angular.module("MyApp", [])
.controller('loginCtrl', ['$scope', '$log', function ($scope, $log) {
}]);
</script>
<div id="loginBox" ng-controller="loginCtrl">
<form action="/" id="userLogin" name="myForm" novalidate>
<label>User id</label>
<input type="text" ng-model="userId" name="myUserid" required="" />
<div ng-show="myForm.$submitted || myForm.myUserid.$touched">
<div ng-show="myForm.myUserid.$error.required">Tell us your name.</div>
</div>
<br /><br />
<input type="submit" name="loginSubmit" ng-click="submit()" value="Login" />
</form>
</div>
You should remove the actions attribute from the form. Like:
<form id="userLogin" name="myForm" novalidate>
You can also add the directive ng-submit in the form and remove the ng-click directive from the submit button.
More information here.
Remove form action .
<form id="userLogin" name="myForm" novalidate>
Here is the plunkr: https://plnkr.co/edit/BchMhCHHETftNuKxBVaj?p=preview
Try adding ng-app to your application. I see no error in having action in form on anything else. checkin those plnkers
Related
I have a simple form with input box.
angular.module('main', [])
.controller('Ctrl', ['$scope', '$location',
function($scope, $location) {
}
]);
.red-border {
border-color: red;
}
<div ng-app="main">
<div ng-controller="Ctrl">
<form name="frm" novalidate="novalidate">
<input type="text" required name="myname" ng-class="{'red-border':frm.myname.$error.$invalid}" />
<br>{{frm | json}}
</form>
</div>
</div>
I need the input box to be in red border at the beginning, since it's already empty, but it doesn't make it red on load.
When I test it like this: ng-class="{'red-box':true}" it does work.
What is the best practice to make it triggered at the very beginning, or do I have to do all workarounds?
Thanks
Edit:
here is my code snippet.
https://jsfiddle.net/Ingeeg/afm7rhsw/8/
Angular only evaluates inputs with the ng-model directive:
<form name="frm" novalidate="novalidate">
<input type="text" required name="myname"
ng-model="myModel"
ng-class="{'red-border':frm.myname.$invalid}" />
</form>
See this working jsfiddle
I need am not able to access the $scope.mainform.subform.submitted property in my script. My html goes as below.
<div ng-form="mainform">
<div ng-repeat="dependent in DependentDetails">
<ng-form name="subform">
<input type="text" class="textbox" ng-model="dependent.FirstName"/>
#*Same goes for other personal details*#
<input type="button" id="submitbutton" value="Save" ng-model="Submit" ng-click="saveDependentDetailsClick($event, $index, dependent)" />
</ng-form>
</div>
</div>
Can anyone help?
First of all, I think that it's not correct to use <ng-form name="subform">...</div> in ng-repeat in a way you have used it because you will end up with multiple forms having same name(subform) within the same scope therefore you can't refer to each of these forms as they have not unique name providing you with reference to them.
You will also end up with multiple submit buttons with the same id="submitbutton" in same html document which is an invalid html.
try this.
var app = angular
.module('MyApp', [
])
.controller('Main', ['$scope', function ($scope) {
$scope.DependentDetails = [{"FirstName":""},{"FirstName":""}];
$scope.saveDependentDetailsClick = function(DependentDetails){
console.log(DependentDetails);
}
}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="main-content" ng-app="MyApp" ng-controller="Main as myCtrl">
<div ng-form="mainform">
<div ng-repeat="dependent in DependentDetails">
<ng-form name="subform">
<input type="text" class="textbox" ng-model="dependent.FirstName"/>
<input type="button" value="Save" ng-click="saveDependentDetailsClick(dependent)" />
</ng-form>
</div>
</div>
</div>
I have a form with some fields (build with ng-repeat, so I dont know how much). I added to the submit button
ng-disabled=form.field.$invalid
and added to all the fields "required" but the ng-disabled work just for this last field. my code:
<form name="form" novalidate>
<div ng-repeat="q in questions">
<textarea name="answer" type="text" ng-model="answers[$index]" required></textarea>
</div>
<button ng-disabled="form.answer.$invalid || form.answer.$pristine" type="submit" ng-click="submit(q)">'Submit'</button>
</form>
how can I validate the user change all fields? thanks!
That is because all your inputs have the same name, hence the behavior due to the form controller instance will have just one property with the name answers and it will point to the last ng-model with that name in the form. Instead provide different names for your inputs using ng-attr-name with probably appending $index and use $invalid property of the form, form will be invalid as long as one of the ng-model(s) (with constraints) within the form is invalid. So just disable the button until the form is invalid since it is a catch-all property on the form controller.
<div ng-repeat="q in questions">
<!-- Provide dynamic form names -->
<textarea ng-attr-name="answer{{$index}}" type="text"
ng-model="answers[$index]" required></textarea>
</div>
<!-- Use form.$invalid -->
<button ng-disabled="form.$invalid || form.$pristine"
type="submit" ng-click="submit(q)">Submit</button>
angular.module('app', []).controller('ctrl', function($scope) {
$scope.questions = ["q1", "q2"]
$scope.answers = {};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<form name="form" novalidate>
<div ng-repeat="q in questions">
<textarea ng-attr-name="answer{{$index}}" type="text" ng-model="answers[$index]" required></textarea>
</div>
<button ng-disabled="form.$invalid || form.$pristine" type="submit" ng-click="submit(q)">Submit</button>
</form>
</div>
I am trying to learn angularJS and have problem figuring out how to solve this issue. I am trying to prepend content of the form when user submit the form. This is my code but it's not working. Can anyone help me to figure out where the problem is? Thanks!
angular.module("displayText", [])
.controller("submitText", function($scope) {
$scope.outputArr = [];
$scope.print = function() {
$scope.outputArr.push($scope.inputText);
$scope.inputText = '';
};
};
});
<body ng-app="displayText">
<div ng-controller="submitText">
<form method="get" action="#">
<input type="text" name="input" ng-model="inputText" required/>
<button ng-click="print(inputText)">Submit</button>
</form>
</div>
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
</body>
put this piece of code inside the submitText controller div
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
then you whole code should be like
<div ng-controller="submitText">
<form method="get" action="#">
<input type="text" name="input" ng-model="inputText" required/>
<button ng-click="print(inputText)">Submit</button>
</form>
<div>
<p ng-repeat="text in outputArr">{{text}}</p>
</div>
</div>
here is the Plunker
in your case the ng-repeat="text in outputArr" is out of the controller that means scope of the submitText controller not accessible for the ng-repeat that's why its not print anything
I know that there are a lot of other similar questions but I didn't find the answer there.
<html ng-app="app" ng-controller="AppController">
...
<form class="navbar-form navbar-left" role="search" ng-submit="alert()">
<div class="form-group">
<input type="text" class="form-control search" ng-model="text" />
</div>
<button type="submit" class="btn btn-default icon-default icon-search">Submit </button>
</form>
</html>
You cant use alert (window.alert) function in angular as you normally do in plain javascript. This is example from angular $window service docs:
<script>
function Ctrl($scope, $window) {
$scope.greeting = 'Hello, World!';
$scope.doGreeting = function(greeting) {
$window.alert(greeting);
};
}
</script>
<div ng-controller="Ctrl">
<input type="text" ng-model="greeting" />
<button ng-click="doGreeting(greeting)">ALERT</button>
</div>
If you put doGreeting (name it as you want) function in your controller and inject $window, then your example should work:
...
<form class="navbar-form navbar-left" role="search" ng-submit="doGreeting()">
...
Try to submit the form by adding ng-click in the button tag and call submit function by passing your form in the ng-click. The function is where you write your logic in the controller. submit