form validation angularjs without submit button

若如初见. 提交于 2019-12-05 08:08:51

Use in $formName.$valid All necessary input fields need required attribute.

<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

To answer your questions:

  • mandatory fields are assessed using the "required" attribute (HTML5).
  • email fields are assessed using the type "email" (HTML5).
  • you can submit with a "submit" type button or with an "ng-click" directive, either.
  • in AngularJS custom validation should be done using directives (see docs).
  • here you have an example of custom validation:

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

try this above, http://www.drtuts.com/handle-form-using-angularjs/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!