0

There is a question with the same subject, but it dont have a conclusive answer.

I have a bunch of inputs with variable names generated by an add jquery function, and i need to validate those fields (if they exist).

Can i use the validate plugin with the class instead of the name? or something?

Here is my code:

//add function

$(document).ready(function(){   

   var i = $('input').size() +1;

   $('#add').click(function(event) {
        event.preventDefault();

$('.table1').before('<table class="new_table">
   '<tr>' +
     '<td colspan="4" class="subTitle_bar">' +
       '<spring:message code="phoneTable" /> '+ i +' '+ 
     '</td>' +
   '</tr>' +

   '<tr>' +
       '<td class="item">' +
          '<spring:message code="phoneNumber"  />' +
          '<span class="required">*</span>' +
       '</td>' +

       '<td colspan="3">' +
          '<input type="text" id="phone1' + i + '" name="phone1' + i + '" class="requiredField phone" />' +
       '</td>' +
   '</tr>');
 i++;
});

And the validate:

$(document).ready(function () {

    $("#contactForm").validate({
        //rules
        rules:{
            phone1:{ required: true },

How can i validate the phone, if i dont know his field name yet?

Can somebody help me?

2 Answers 2

1

Few ideas, in order of what I think makes sense. - Adding required attribute to the inputs. - Adding class "required" - Adding the rules with js after you make the form.

but in short just add < input id="phone" type="text" name="email" required / >

He does that in the first example on page http://validation.bassistance.de/documentation/

I don't know this for sure but maybe you need to call $("#contactForm").validate() after you make the form. ( not sure if it matters or not )

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

2 Comments

Thanks a lot, its worked, i need also to validate the type of content, where can i define in the plugin this kind of validation? like a numeric field: < input id="phone" type="text" name="email" digits / >
I would not have only numeric since +354ddddddd is valid for phones. but he does have an example validation.bassistance.de/phoneUS-method but that's US phones. to use that you need ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/… in the code you can see few other phone validation rules. so you need to pick one
0

If you are just needing the field to be required you can just add a class of required to the element you're generating... There are also other options for making fields equal to eachother and whatnot.

"There are several ways to specify validation rules. Validation methods without parameters can be specified as classes on the element (recommended)" - FROM Documentation

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.