Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. <form method="post" id="loginform" name="loginform" ng-submit="loginUser()" novalidate>
  2. <div>
  3. {{message}}
  4. </div>
  5. <div>
  6. <label>User Name</label>
  7. <input type="text" id="txtUserName" ng-model="user.UserName" name="user.UserName" />
  8. </div>
  9. <div>
  10. <label>Password</label>
  11. <input type="text" id="txtPassword" ng-model="user.Password" name="user.Password" />
  12. </div>
  13. <div>
  14. <input type="submit" id="btnLogin" title="Save" name="btnLogin" value="Login" />
  15. </div>
  16. </form>
  17.  
  18. var demoApp = angular.module('demoApp', []);
  19.  
  20. demoApp.controller("homeController", ["$scope", "$timeout", function ($scope, $timeout) {
  21.  
  22. $scope.loginUser = function () {
  23.  
  24. var form = document.getElementById("loginform");
  25. //var form = $scope.loginform; - tried this here...
  26. //var form = $scope["#loginform"]; tried this
  27. //var form = angular.element(event.target); - tried this...
  28. // tried a lot of other combinations as well...
  29.  
  30. form.attr("method", "post");
  31. form.attr("action", "Home/Index");
  32. form.append("UserName", $scope.user.UserName);
  33. form.append("Password", $scope.user.Password);
  34. form.append("RememberMe", false);
  35. form.submit();
  36. };
  37. }]);
  38.  
  39. $scope.loginUser = function () {
  40. if($scope.loginform.$valid){
  41. user.rememberme=false;
  42.  
  43. $http({
  44. url: 'Home/Index',
  45. method: "POST",
  46. data: user
  47. })
  48. .then(function(response) {
  49. // success
  50. },
  51. function(response) { // optional
  52. // failed
  53. });
  54.  
  55. }
  56. };
  57.  
  58. <form method="post" id="loginform" name="loginform" ng-submit="loginUser()" novalidate>
  59. <div>
  60. {{message}}
  61. </div>
  62. <div>
  63. <label>User Name</label>
  64. <input type="text" id="txtUserName" required ng-model="user.UserName" name="UserName" />
  65. </div>
  66. <div>
  67. <label>Password</label>
  68. <input type="text" id="txtPassword" ng-model="Password" name="user.Password"required />
  69. </div>
  70. <div>
  71. <input type="submit" ng-disabled="myForm.UserName.$invalid || myForm.Password.$invalid" id="btnLogin" title="Save" name="btnLogin" value="Login" />
  72. </div>
  73. </form>
  74.  
  75. <form id="loginform" name="loginform" ng-submit="loginUser()">
  76. <div>
  77. {{message}}
  78. </div>
  79. <div>
  80. <label>User Name</label>
  81. <input type="text" id="txtUserName" ng-model="user.UserName" name="user.UserName" required/>
  82. <div class="help-block" ng-messages="loginform.txtUserName.$error" ng-show="loginform.txtUserName.$touched">
  83. <p ng-message="required">Username is required.</p>
  84. </div>
  85. </div>
  86. <div>
  87. <label>Password</label>
  88. <input type="text" id="txtPassword" ng-model="user.Password" name="user.Password" required/>
  89. <div class="help-block" ng-messages="loginform.txtPassword.$error" ng-show="loginform.txtPassword.$touched">
  90. <p ng-message="required">Password is required.</p>
  91. </div>
  92. </div>
  93. <div>
  94. <input type="submit" id="btnLogin" title="Save" name="btnLogin" value="Login" ng-click="loginUser()" />
  95. </div>
  96. </form>
  97.  
  98. var demoApp = angular.module('demoApp', ['ngMessages']);
  99.  
  100. demoApp.controller("homeController", ["$scope", "$timeout", function ($scope, $timeout) {
  101.  
  102. $scope.loginUser = function () {
  103. if($scope.loginform.$valid){
  104. //Code to run before submitting (but not validation checks)
  105. } else{
  106. return false;
  107. }
  108. };
  109. }]);
  110.  
  111. <div ng-controller="homeController" ng-init="construct()">
  112. <form method="post" action="Index" role="form" id="loginform" name="loginform" ng-form-commit novalidate class="ng-pristine ng-invalid ng-invalid-required">
  113. <div class="form-group">
  114. <label for="UserName">User ID</label>
  115. <input autocomplete="off" class="form-control ng-valid ng-touched ng-pristine ng-untouched ng-not-empty"
  116. id="UserName" name="UserName" ng-model="user.UserName" type="text" value=""
  117. ng-change="userNameValidation = user.UserName.length == 0">
  118. <span class="field-validation-error text-danger" ng-show="userNameValidation">The User ID field is required.</span>
  119. </div>
  120.  
  121. <div class="form-group">
  122. <label for="Password">Password</label>
  123. <input autocomplete="off" class="form-control ng-valid ng-touched ng-pristine ng-untouched ng-not-empty"
  124. id="Password" name="Password" ng-model="user.Password" type="password" value=""
  125. ng-change="passwordValidation = user.Password.length == 0">
  126. <span class="field-validation-error text-danger" ng-show="passwordValidation">The Password field is required.</span>
  127. </div>
  128.  
  129. <div>
  130. <input type="button" id="btnLogin" title="Login" name="btnLogin" value="Login" ng-click="validateUser(loginform)" />
  131. </div>
  132. </form>
  133. </div>
  134.  
  135. var demoApp = angular.module('demoApp', []);
  136.  
  137. demoApp.factory("commonService", function () {
  138. return {
  139. isNullOrEmptyOrUndefined: function (value) {
  140. return !value;
  141. }
  142. };
  143. });
  144.  
  145. //This is the directive that helps posting the form back...
  146. demoApp.directive("ngFormCommit", [function () {
  147. return {
  148. require: "form",
  149. link: function ($scope, $el, $attr, $form) {
  150. $form.commit = function () {
  151. $el[0].submit();
  152. };
  153. }
  154. };
  155. }]);
  156.  
  157. demoApp.controller("homeController", ["$scope", "commonService", function ($scope, commonService) {
  158.  
  159. $scope.construct = function construct() {
  160. $scope.user = { UserName: "", Password: "" };
  161. };
  162.  
  163. $scope.userNameValidation = false;
  164. $scope.passwordValidation = false;
  165. $scope.isFormValid = false;
  166.  
  167. $scope.validateUser = function ($form) {
  168. $scope.isFormValid = true;
  169.  
  170. $scope.userNameValidation = commonService.isNullOrEmptyOrUndefined($scope.user.UserName);
  171. $scope.passwordValidation = commonService.isNullOrEmptyOrUndefined($scope.user.Password);
  172.  
  173. $scope.isFormValid = !($scope.userNameValidation || $scope.passwordValidation);
  174.  
  175. if ($scope.isFormValid === true) {
  176. $scope.loginUser($form);
  177. }
  178. };
  179.  
  180. $scope.loginUser = function ($form) {
  181. $form.commit();
  182. };
  183. }]);
  184.  
  185. (function(angular) {
  186. 'use strict';
  187.  
  188. function DemoFormCtrl($timeout, $sce) {
  189. var ctrl = this;
  190. this.$onInit = function() {
  191. this.url = $sce.trustAsResourceUrl(this.url);
  192. /*$timeout(function() {
  193. ctrl.form.$$element[0].submit();
  194. });*/
  195. };
  196.  
  197. this.validate = function(ev) {
  198. console.log('Running validation.');
  199. if (!this.form) {
  200. return false;
  201. }
  202. };
  203. }
  204.  
  205. angular.module('app', [])
  206. .component('demoForm', {
  207. template: `
  208. <p>To run this demo allow pop-ups from https://plnkr.co</p>
  209. <hr>
  210. <p>AngularJS - submit form programmatically after validation</p>
  211. <form name="$ctrl.form" method="get" target="blank" action="{{::$ctrl.url}}" novalidate
  212. ng-submit="$ctrl.validate($event)">
  213. <input type='hidden' name='q' ng-value='::$ctrl.value'>
  214. <input type='hidden' name='oq' ng-value='::$ctrl.value'>
  215. <input type="submit" value="submit...">
  216. </form>`,
  217. controller: DemoFormCtrl,
  218. bindings: {
  219. url: '<',
  220. value: '<'
  221. }
  222. });
  223.  
  224. })(window.angular);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement