简体   繁体   中英

Angular JS textarea validation with required & showing error message

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

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> 

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