Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am using jQuery validation library for validating my forms created using Angular.JS. I am able to add validations on all static/known input fields in the form.

Along with fix inputs I am going to have some dynamic questions in the form. These questions are going to get rendered using ng-repeat directive.

I am able to validate input only if id and name of the input control are known. In case of dynamic control we can not predict the same.

HTML Code:

<body>
    <div ng-controller="PersonController">
        <div ng-repeat="social in formData.socials">
            <ng-form name="urlForm">
                  <input type="url" name="socialUrl" ng-model="social.url">
                  <span class="alert error" ng-show="urlForm.socialUrl.$error.url">URL error</span>
            </ng-form>
        </div> 
        <hr>

        <form id="contact-form">
          <div class="control-group">
            <label class="control-label" for="email">Email Address</label>
            <div class="controls">
                <input type="text" name="email" id="email" placeholder="Your email address">
            </div>
          </div>
          <br/>
          <div ng-repeat="question in Questions">
            <label class="control-label" for="email">{{question.Title}}</label>
            <div class="controls">
              <input name="question">
            </div>
            <br/>
          </div> 
        </form>
    </div>

</body>

I have checked the approach of validating dynamic input control using ng-form element. But I want to validate the form using jQuery validation only as I am going to have jQuery bootstrap popovers which works along with jQuery Validation only.

Javascript:

angular.module('directive', []).controller('PersonController', function personController($scope) {
    $scope.loading = false;
    $scope.formData = {
        socials: [{
            url: 'http://www.valid.com'},
        {
            url: 'invalid url'}]
    };

    $scope.Questions = [
            { "Title": "Whats your name?", "Type": "Text" },
            { "Title": "Whats your birthdate?", "Type": "Date" }
        ];

    $(document).ready(function () {

      $('#contact-form').validate({
          rules: {
              email: {
                  required: true,
                  email: true
              }
          },
          highlight: function (element) {
              $(element).closest('.control-group').removeClass('success').addClass('error');
          },
          success: function (element) {
              element.text('OK!').addClass('valid')
                  .closest('.control-group').removeClass('error').addClass('success');
          }
      });

    });

});

Please refer this: http://plnkr.co/edit/3jiYucTKOlW4G0TISFNj?p=preview

How can I add jQuery validation rule for dynamic element? Please help me out. Thanks in advance.

share|improve this question

marked as duplicate by Sparky Oct 22 at 15:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

I don't know why you are taking jQuery validation for angularJs. Angular have a built in support for form validation and that gives you more flexibility in your development. Please refer the documentation ( https://docs.angularjs.org/guide/forms )

share|improve this answer
    
As I said in the question I am going to use jQuery bootstrap popover. This fits with jQuery validation –  Ajinkya Oct 22 at 10:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.