2014-09-22 144 views
4

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

<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本身中对表单进行客户端验证吗?

感谢

回答

2

在$表格名称。使用$有效 所有必需的输入字段需要必需的属性。

<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

+0

领域的验证未与您的解决方案工作。我给了一个表单的名称和我添加的ng-disabled =“myForm。$ valid”的按钮。 – 2014-09-22 08:53:12

+0

@PraveenReddy'ng-disabled =“!myForm。$ valid”' – karaxuna 2014-09-22 08:57:08

+0

抱歉,我错过了!之前的代码,但仍然按钮“创建新用户”被禁用,当我点击按钮控制器中的方法被调用没有任何验证。 – 2014-09-22 09:01:39

0

回答您的问题:

  • 必填字段使用的是 “必需的” 属性(HTML5)评估。
  • 使用“email”类型(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> 
+1

这里只检查长度。要求是,点击按钮后,只有表单验证应该开始,即必需的字段验证,电子邮件验证等。 – 2014-09-23 08:45:30

0
<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/

+0

尽管这段代码可能有助于解决问题,但它并没有解释_why_和/或_how_它是如何回答问题的。提供这种附加背景将显着提高其长期教育价值。请[编辑]您的答案以添加解释,包括适用的限制和假设。 – 2016-08-22 14:01:30