2016-10-04 38 views
-2

我需要驗證登錄功能。我添加了我的Login.html文件和LoginController.js文件。我想驗證這從使用這個JavaScript。這是angularjs代碼。之後,我必須爲此代碼設置php網址。如何驗證表單使用if語句中的javascript角

的login.html

$scope.submitForm = function() { 
 

 
    \t \t var user_email = user_email; 
 
    \t \t var user_password = user_password; 
 

 
    \t \t if(user_email && user_password){ 
 

 
    \t \t \t console.log(user_email); 
 
     
 
    \t \t } 
 

 
    \t \t 
 

 
    \t };
<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>

+0

我猜你以前沒有做任何研究嗎? –

+0

我沒有任何想法請你給我回答 – moni123

+0

https://scotch.io/tutorials/angularjs-form-validation –

回答

0

在你提供的代碼所做的更改,對註冊/登錄的點擊就可以調用submitForm()功能通過ng-click評估形式values.Check下面的工作模型代碼片段。

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>

+0

'ng-click'然後我添加了這個。它不會工作 – moni123

+0

你有沒有試過我提供的代碼片段在這裏工作! –