简体   繁体   中英

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

Circular dependency found: tooltipDirective <- tooltipDirective

I have read the documentation and don't understand it-am I suppose to somehow reference ng-validate in my controller? 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://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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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