简体   繁体   中英

How to make a button clickable only if the textfield is entered

I had a textfield to enter the emailId..Iam getting that value stored in $rootScope.emailId . Now when I click the submit button,I must take to next page only if the emailId is entered or else it must give an alert saying that must be filled. Currently it is in form and i am using required="true" and form.$valid but still its not working is there any other way to achieve this.

<form name="customerForm" class="form-horizontal">
    <div class="form-group">
        <label for="inputEmail" class="col-lg-4 col-lg-offset-1  control-label">Email address</label>
        <div class="col-lg-4">
            <input class="form-control" ng-model="$root.customerDetails.eMail" placeholder="Enter customer email" type="email" required focus value="{{emailId}}">
        </div>
    </div>
</form>

--

<div class="col-lg-1">
    <button type="submit" class="btn btn-primary right-button" ng-click="$ctrl.proceedToPaymentDetails()" ng-disabled="$ctrl.customerDetails">Next</button>
</div>

Currently the form and the button are not in the same html page..Is der any way to fix this.

更改ng-disabled="customerForm.$invalid"或者您也可以在procedToPaymentDetails()方法内检查电子邮件ID的值以获取警报消息。

Please gone throw this example code for your better understanding

<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form novalidate>
    First Name:<br>
    <input type="text" ng-model="user.firstName"><br>
    Last Name:<br>
    <input type="text" ng-model="user.lastName">
    <br><br>
        Email:<br>
    <input type="text" ng-model="user.emailID">
    <br><br>
    <button ng-click="proceedToPaymentDetails()">Next</button>
  </form>
  <p>form = {{user}}</p>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.user = {firstName:"", lastName:"",emailID:""};
    $scope.proceedToPaymentDetails = function() {
        alert($scope.user.emailID);
    };
});
</script>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM