mateusz-c
mateusz-c

Reputation: 110

Angular JS ng-click validation

I want to have that after clicking submit button it check if there is something written down in input form if not the ng-class change the span color to red . I want to have red color only after submittion not all the time , here is link :

<form name="myForm" validate class="form-horizontal">

  <div class="control-group" >
    <label ng-class="{bad: ( myForm.name.$invalid && myForm.name.$pristine )  }"  >Name:</label>
    <input type="text" name="name" placeholder="Name" ng-model="user.name" required/>
    <span ng-show="isInvalid('name')" class="help-inline">Name is required</span>
    <span ng-show="isValid('name')">Great!</span>
  </div>

  <button type="submit">SUB</button>

</form>

http://plnkr.co/edit/m3dqBnpPenbY65xa3PXH?p=preview

Upvotes: 0

Views: 5032

Answers (1)

Kalhan.Toress
Kalhan.Toress

Reputation: 21901

you need to track whether submit button clicked or not , here u can maintain a scope variable and detect the form is submitted or not

<button type="submit" ng-click="submitted = true">SUB</button>

if some one clicked the submit then, there is a scope variable called submitted with true value

<label ng-class="{bad: ( myForm.name.$invalid && myForm.name.$pristine && submitted )  }"  >Name:</label>

here is the Plunker

Upvotes: 1

Related Questions