【问题标题】:How to validate form using if statement javascript in angular如何在 Angular 中使用 if 语句 javascript 验证表单
【发布时间】:2016-10-04 18:38:57
【问题描述】:

我需要验证登录功能。我添加了我的 Login.html 文件和 LoginController.js 文件。我想通过使用这个 javascript 来验证这一点。这是 angularjs 代码。之后我必须为此代码设置 php url。

Login.html

$scope.submitForm = function() {

  		var user_email = user_email;
  		var user_password = user_password;

  		if(user_email && user_password){

  			console.log(user_email);
        
  		}

  		

  	};
<div ng-controller="LoginController">
        <form name="userForm" ng-submit="submitForm()">

        <div class="list">
          <label class="item item-input">
            <span class="input-label">Username</span>
            <input type="text" name="user_email" ng-model="user.user_email" required>
          </label>
          <label class="item item-input">
            <span class="input-label">Password</span>
            <input type="password" name="user_password" ng-model="user.user_password" required>
          </label>
        </div>

        <span ng-show="userForm.user_email.$touched && userForm.user_email.$invalid">The Username is required.</span> </br>
        <span ng-show="userForm.user_password.$touched && userForm.user_password.$invalid">The Password is required.</span>

        <button class="button button-block button-positive activated">Sign In</button>
        <button onclick="location.href='#/login/:friendId/register/';"  class="button button-block button-positive activated">Register</button>
        
        </form>
      </div>

【问题讨论】:

  • 我猜你之前没有做过任何研究?
  • 我不知道请你给我答案
  • var x = x; - 保证 x 被分配 undefined
  • 我知道如何验证前端,但我需要使用 javascript 进行验证

标签: javascript jquery html angularjs


【解决方案1】:

在您提供的代码中进行了更改,单击注册/登录后,您可以通过ng-click 调用submitForm() 函数来评估表单值。检查下面的代码sn-p 以获得工作模型。

angular.module('myApp', ['ng'])

.controller('LoginController', ['$scope',
  function($scope) {
    $scope.user = {};
    $scope.submitForm = function() {

      var user_email = $scope.user.user_email;
      var user_password = $scope.user.user_password;

      if (user_email && user_password) {

        console.log(user_email);

      }
    };
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="LoginController">
  <form name="userForm">

    <div class="list">
      <label class="item item-input">
        <span class="input-label">Username</span>
        <input type="text" name="user_email" ng-model="user.user_email" required>{{user.user_email}}
      </label>
      <label class="item item-input">
        <span class="input-label">Password</span>
        <input type="password" name="user_password" ng-model="user.user_password" required>{{user.user_password}}
      </label>
    </div>

    <span ng-show="userForm.user_email.$touched && userForm.user_email.$invalid">The Username is required.</span> 
    </br>
    <span ng-show="userForm.user_password.$touched && userForm.user_password.$invalid">The Password is required.</span>

    <button class="button button-block button-positive activated">Sign In</button>
    <button ng-click="submitForm()" class="button button-block button-positive activated">Register</button>

  </form>
</div>

【讨论】:

  • 你试过我提供的sn-p吗!
猜你喜欢
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 2019-08-13
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
相关资源
最近更新 更多