【问题标题】:HTML5 validation with a link and angular.js带有链接和 angular.js 的 HTML5 验证
【发布时间】:2017-10-24 12:29:36
【问题描述】:

我有一个简单的表格,分为 3 个部分。在第一部分,我收集名字、姓氏和电子邮件。

我正在尝试使用type="email"required 来提供验证反馈,但是我直到表格的最后一部分才使用input submit

表单分为三个部分,每个部分只有在用户点击后才能看到

<a ui-sref="form.select" class="btn btn-block btn-info" >
            Next Section <span class="glyphicon glyphicon-circle-arrow-right"></span>
        </a>

我希望这个点击触发通常提交方式的验证。

甚至在用户填写字段之前禁用此按钮。

这是完整的表单代码

<div class="form-group">
    <label for="first-name">First Name*</label>
    <input type="text" class="form-control" name="Fname" placeholder="Enter first name" ng-model="formData.Fname" required>
</div>

<div class="form-group">
    <label for="last-name">Last Name*</label>
    <input type="text" class="form-control" name="Lname" placeholder="Enter last name" ng-model="formData.Lname" required>
</div>

<div class="form-group">
    <label for="email">Email*</label>
    <input type="email" class="form-control" name="email" placeholder="Enter a valid email address" ng-model="formData.email" required>
</div>

<div class="form-group row">
    <div class="col-xs-6 col-xs-offset-3">
        <a ui-sref="form.select" class="btn btn-block btn-info" >
            Next Section <span class="glyphicon glyphicon-circle-arrow-right"></span>
        </a>
    </div>
</div>

问题

所以我的问题是,如何在单击按钮时进行 HTML 5 验证,以及如何在正确填充字段之前禁用按钮?

【问题讨论】:

    标签: javascript angularjs html validation


    【解决方案1】:

    如果你想禁用按钮,直到所有字段都没有插入有效数据,试试这个

    <a ui-sref="form.select" class="btn btn-block btn-info" ng-
     disabled="formData.$invalid" >Next Section <span class="glyphicon glyphicon-circle-arrow-right"></span>
    </a>
    

    【讨论】:

    【解决方案2】:

    您可以将下一节链接更改为&lt;button&gt; 并使用ng-disabledng-disabled 不适用于 &lt;a&gt; 元素。 要么在ng-disabled 中写下所有条件,要么如果它变得太长/太复杂,写一个控制器函数来检查。

    <button ui-sref="form.select" ng-disabled="myform.first_name.$invalid || ....">
        Next Section 
    </button>
    
    <button ui-sref="form.select" ng-disabled="isButtonDisabled()">
        Next Section
    </button>
    

    如果你必须使用&lt;a&gt;,你必须自己禁用链接。例如:

    .disabled {
      cursor: not-allowed;
    }
    
    <a ng-click="disabled()" ng-class="{disabled: addInviteesDisabled()}">Add</a>
    
    $scope.disabled = function() {
      if($scope.addInviteesDisabled) { return false;}
    }    
    

    【讨论】:

    • 我可以使用没有问题的按钮,我会研究使用 ng-disabled="myform.first_name.$invalid || ...." 因为我认为这是最好的解决方案
    【解决方案3】:

    您可以以此为参考:

     <form name="upSign">
    
            First Name:
              <input type="text" name="firstName" placeholder="Enter your first name" ng-model="user.first_name" required>
    
    
          Last Name:
              <input type="text" name="lastName" placeholder="Enter your last name" ng-model="user.last_name" required>
    
    
          Email:
              <input type="email" name="mailId" ng-model="user.email_id" placeholder="Enter your Email" required>
    
        </form>
    
        <p ng-show="upSign.firstName.$invalid || upSign.mailId.$invalid || upSign.lastName.$invalid" style="color: red">*  Invalid</p>
    
    
        <button class="button button-full button-positive" ng-disabled="upSign.firstName.$invalid || upSign.mailId.$invalid || upSign.lastName.$invalid || upSign.firstName.$untouched || upSign.mailId.$untouched || upSign.lastName.$untouched" ng-click="register()">Register Me</button>
    

    【讨论】:

    • 这不起作用,因为它触发提交而不是下一部分
    • 但是如果禁用它会如何触发提交呢?
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多