0

I have a Angular JS code where i want to implement validation and focus on the field back if textarea is empty.

<form ng-submit="addComment()" role="form" name="blogForm" novalidate>
    <div class="form-group" ng-class="{ 'has-error' : blogForm.blogComment.$invalid && replySubmitted }">
          <textarea ng-model="blogComment" name="blogComment" class="form-control" rows="3"  ng-required="true" required></textarea>
    </div>
    <div style="color:#a94442;" ng-show="blogForm.blogComment.$invalid && replySubmitted">
         <span ng-show="blogForm.blogComment.$error.required">Comment Text required.</span>
    </div>
    <button type="submit"  ng-click="replySubmitted = true" class="btn btn-primary blog-bubmit-button">Submit</button>
</form>

For some reasons the above code is just inserting empty data and field is focused Invalid. Any Suggestions may help me. Thanku

1 Answer 1

1

you have define ng-app to process it as angular block.

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app=''>
  <form ng-submit="addComment()" role="form" name="blogForm" novalidate>
    <div class="form-group" ng-class="{ 'has-error' : blogForm.blogComment.$invalid && replySubmitted }">
      <textarea ng-model="blogComment" name="blogComment" class="form-control" rows="3" ng-required="true" required></textarea>
    </div>
    <div style="color:#a94442;" ng-show="blogForm.blogComment.$invalid && replySubmitted">
      <span ng-show="blogForm.blogComment.$error.required">Comment Text required.</span>
    </div>
    <button type="submit" ng-click="replySubmitted = true" class="btn btn-primary blog-bubmit-button">Submit</button>
  </form>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.