简体   繁体   English

如何使输入字段强制为angular? ng-validate或html require无效

[英]How to make input fields mandatory in angular ? ng-validate or html require does not work

I am trying to require user to fill out input fields /have a certain length. 我试图要求用户填写输入字段/具有一定的长度。 I tried simply - require, I tried form$valid with class="validate" and for the length I used angular ng-validate. 我尝试了一下-需要,我尝试了使用class =“ validate”的form $ valid,并使用了角度ng-validate。 Nothing works and it works in other forms I have. 什么都行不通,它以我拥有的其他形式行事。 Ive been looking at my code, comparing for a while and just don't know why its not working (even if all fields are empty, I am being directed to another page which should only happen in case validation is positive). 我一直在看我的代码,比较了一段时间,只是不知道为什么它不起作用(即使所有字段为空,我也被定向到另一个页面,只有在验证为肯定的情况下才应该发生)。 Also for the ng-validate, once I start typing Im getting the error: 同样对于ng-validate,一旦我开始输入Im就会收到错误:

Circular dependency found: tooltipDirective <- tooltipDirective 找到循环依赖项:tooltipDirective <-tooltipDirective

I have read the documentation and don't understand it-am I suppose to somehow reference ng-validate in my controller? 我已经阅读了文档,但听不懂-我想以某种方式在控制器中引用ng-validate吗? Thanks!! 谢谢!!

<div>
   <form id="paymentForm" method="POST" ng-hide ='paid' validation-form>
      <div>
         <label for="name">first</label>
         <input type="text" name="name" validation-field required minlength="3" placeholder= 'First Name' ng-model = "info.firstName" style="border-bottom:1px solid pink;"></input>
      </div>
      <div>
         <label for="userEmail">last</label>
         <input placeholder= 'Last Name' ng-model = "info.lastName" required></input>
      </div>
   </form>
   <button type="submit"class="submitValidate" ng-hide ='paid' ng-click='validate()' material>Submit</button> 
</div>

i recommand you to read about angular form validation 我建议您阅读有关角度形式验证的信息

https://scotch.io/tutorials/angularjs-form-validation or https://scotch.io/tutorials/angularjs-form-validation

https://docs.angularjs.org/guide/forms https://docs.angularjs.org/guide/forms

an example for required inputs and disabling submit button while this input are not filled 必需输入的示例,在未填写此输入时禁用提交按钮

https://plnkr.co/edit/12xpTay5O4Qczyk1CNmR?p=preview https://plnkr.co/edit/12xpTay5O4Qczyk1CNmR?p=preview

   <form name="form" ng-submit="validate()" role="form">
        <div class="form-group" ng-class="{ 'has-error': form.name.$dirty && form.name.$error.required }">
            <label for="name">name</label>
            <input type="text" name="name" id="name" class="form-control" ng-model="user.name" required />
            <span ng-show="form.name.$error.required" class="help-block">Name is required</span>
        </div>
        <div class="form-group" ng-class="{ 'has-error': form.email.$dirty && form.email.$error.required }">
            <label for="email">Email</label>
            <input type="text" name="email" id="email" class="form-control" ng-model="user.email" required />
            <span ng-show="form.email.$error.required" class="help-block">Email is required</span>
        </div>
        <div class="form-actions">
            <button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">
                validate
            </button>

        </div>
        <pre>{{form |json }}</pre>

    </form>
ng-minlength="3"

Try something like this : 尝试这样的事情:

<form name="myForm" ng-submit="count = count + 1" ng-init="count=0" ng-app>
 <div class="control-group" ng-class="{error: myForm.name.$invalid}">
        <div class="controls">
            <input type="text" name="name"  placeholder="First Name" ng-model="name" ng-minlength="3"  required />
             <span ng-show="myForm.name.$error.minlength" class="help-inline">Name should be minimum 3</span>   
        </div>
    </div>

     <div class="control-group">
        <div class="controls">                       
            <input  class="btn" type="submit" value ="submit" />
        </div>

         count: {{count}}<br />

<tt>myForm.$invalid = {{myForm.$invalid}}</tt><br/>
    </div>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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