Solved

how do I reference jquery validate on dynamic fields

Posted on 2014-04-20
1
681 Views
Last Modified: 2014-04-21
Hi,

I'm using jquery validate on email inputs that a user can add/subtract fields. I'm not sure how I can get the jquery validate function to validate the email addresses with dynamically generated fields ... Example below of 2 fields but it can be "p_new_x" depending on x amount of field additions.

<input type="email" name="p_new_1" id="p_new_1" class="form-control" placeholder="Enter Email Address" required/>

<input type="email" name="p_new_2" id="p_new_2" class="form-control" placeholder="Enter Email Address" required/>

Open in new window


$("#sharingForm").validate({

        submitHandler: function(form) {

            var data = $("#sharingForm").serialize();

            $.ajax({
                type: "POST",
                url: "app/func/func_sharing.cfc?method=sendInvite",
                data: data,
                cache: false,
                success: function() {
                    refreshSharingContent();
                },
                error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
                }
            });
        }

    });

Open in new window

0
Comment
Question by:brihol44
1 Comment
 
LVL 26

Accepted Solution

by:
skullnobrains earned 250 total points
ID: 40011703
something like this (probably a few typos)

$('.form-control').each(function(){
  if(0 == $(this).attr('name').indexOf('p_new'))validateEmail($(this));
});

or use an ereg
if($(this).attr('name').match('^p_new'))

it would be simpler to add a specific class (validateEmail for example) to all form form controls that need email validation
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question