【问题标题】:form validation angularjs without submit button没有提交按钮的表单验证angularjs
【发布时间】:2014-09-22 08:32:58
【问题描述】:

我想要验证没有提交按钮的表单。

    <div ng-controller="UserCreationCtrl">
        <form novalidate="novalidate" class="form-horizontal">
            <div class="control-group">
                <label class="control-label" for="inputUserLogin">User Login:</label>

                <div class="controls">
                    <input type="email" id="inputUserLogin" ng-model="user.userLogin" placeholder="User Login" required />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputFirstName">First name:</label>

                <div class="controls">
                    <input type="text" id="inputFirstName" ng-model="user.firstName" placeholder="First name"/>
                </div>
            </div>

            <div>
                <span  class="nullable">
                    <select ng-model="user.lookupId" ng-options="select as speciality.lookupName for speciality in specialityList" ng-change="selectedSpecilaty()">
                        <option value="">-- choose speciality --</option>
                    </select>
                </span>
            </div>
            <div> some extra other fields goes here</div>
            <div class="control-group">
                <div class="controls">
                    <a ng-click="cancel()" class="btn btn-info btn-xs">cancel</a>
                    <a ng-click="createNewUser()" class="btn btn-small btn-primary">create new user</a>
                </div>
            </div>

        </form>
    </div>

如何将userlogin(email)firstnamespeciality 字段设为必填,email 字段必须作为电子邮件进行验证。通常我们如何对没有提交按钮的表单进行验证。即,当我单击“创建新用户”链接时,应验证表单。

当点击链接后出现错误时,表单应该有正确的字段数据,并且应该只在错误字段旁边显示错误消息或以红色字段显示。

我们可以在没有控制器的情况下在 html 中对表单进行客户端验证吗?

谢谢

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    在 $formName.$valid 中使用 所有必要的输入字段都需要 required 属性。

    <form novalidate="novalidate" class="form-horizontal" name='myForm'>
     <a ng-click="createNewUser()" class="btn btn-small btn-primary" ng-disabled="!myForm.$valid">create new user</a>
    </form>
    

    http://plnkr.co/edit/6zVqTeW1WARIUcbS7dC9?p=preview

    【讨论】:

    • 字段验证不适用于您的解决方案。我为表单命名,并为按钮添加了 ng-disabled="myForm.$valid"。
    • @PraveenReddy ng-disabled="!myForm.$valid"
    • 对不起,我错过了!在代码之前,但“创建新用户”按钮仍然被禁用,当我点击按钮时,控制器中的方法被调用而没有任何验证。
    • 你可以使用任何你喜欢的 $invaid 或 $valid 由你决定:).updated with plukner
    • 这不是我的要求,因为它会禁用按钮。该按钮不应被禁用。在我点击按钮后,它必须在不丢失现有字段数据的情况下显示错误字段。
    【解决方案2】:

    回答您的问题:

    • 必填字段使用“必需”属性 (HTML5) 进行评估。
    • 使用“电子邮件”(HTML5) 类型评估电子邮件字段。
    • 您可以使用“提交”类型的按钮或“ng-click”指令提交。
    • 在 AngularJS 中自定义验证应该使用指令来完成(参见docs)。
    • 这里有一个自定义验证的示例:

    <form name="form" class="css-form" novalidate>
      <div>
        Size (integer 0 - 10):
        <input type="number" ng-model="size" name="size"
               min="0" max="10" integer />{{size}}<br />
        <span ng-show="form.size.$error.integer">This is not valid integer!</span>
        <span ng-show="form.size.$error.min || form.size.$error.max">
          The value must be in range 0 to 10!</span>
      </div>
    
      <div>
        Length (float):
        <input type="text" ng-model="length" name="length" smart-float />
        {{length}}<br />
        <span ng-show="form.length.$error.float">
          This is not a valid float number!</span>
      </div>
    </form>
    

    【讨论】:

    • 这里只检查长度。要求是单击按钮后,仅应启动表单验证,即必填字段验证、电子邮件验证等。
    【解决方案3】:
    <form name="form_validation" ng-submit="form_validation.$valid && submit()" novalidate> <div ng-show="form_validation.$submitted || form_validation.to_address.$touched">
       <div class="alert alert-warning" ng-show="form_validation.to_address.$error.required || form_validation.to_address.$error.email"><a href="#" class="close" data-dismiss="alert">&times;</a>Enter valid Email Addresses.</div>
     </div>
     <div class="form-group">
        <label for="ToAddress"> To Address: </label>
        <input type="email" name="to_address" id="to_address" class="form-control" ng-model="user.to_address" ng-required="true"/>
    </div>
    

    在上面试试这个, http://www.drtuts.com/handle-form-using-angularjs/

    【讨论】:

    • 虽然这段代码可能有助于解决问题,但它并没有解释为什么和/或如何回答问题。提供这种额外的背景将显着提高其长期教育价值。请edit您的回答添加解释,包括适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    相关资源
    最近更新 更多