-2

I have a dropdown which has countries and in the same form on 2nd line I have ZipCode texbox. I want to limit the minLenght of the zipcode field dynamically. For example If US is slected zipcode should not be less than 5 and for other countries some other minLength Limit. My form uses a parsley validator

I wrote below function onChange event of my country dropdown

function setZipLimit() {
    alert("in Zip limit" + document.getElementById("billingCountry").value);
    if (document.getElementById("billingCountry").value == "US") {
        alert("I am in US");
        $('#billingForm').parsley('destroy');
        $('#billingPostalCode').data('minlength', 3);
        $('#billingForm').parsley();
    } else {
        $('#billingForm').parsley('destroy');
        $('#billingPostalCode').data('minlength', 10);
        $('#billingForm').parsley();
    }
}

But this doesnt seem to be helping. Can you please help me?

dvdgsng
  • 1,691
  • 16
  • 27
  • 1
    I don't know parsley, but looking at how you specify things in their doc, it looks to me like you would use `$('#billingPostalCode').attr('parsley-minlength', 3);`, not `$('#billingPostalCode').data('minlength', 3)`. – jfriend00 Nov 28 '13 at 04:02

1 Answers1

0

You cant just edit the Attribute, because parsley.js initially Reads the form and gets its Values. Changing them Afterwards shouldnt have any Effect.

However you can use the Parsley.js API for Javascript functions to Edit the constraints. You already used them to destroy and Build Parsley. Parsley got the following Function: $( '#field' ).parsley( 'updateConstraint', { minlength: 6 } );

There are also some with addConstraint and removeConstraint in the Documentation: http://parsleyjs.org/documentation.html

If your Form gets more complex and you have several of those dynamic cases, i recommend to only Destroy and Rebuild specific Parts of your Form. The Reason is (from an issue I had recently): If you Destroy Parsley, change some Fields, build Parsley and then Destroy it again because another if Statement takes place, your previous Changes get thrown away, because Build only reads the js inbetween the Destroy and Build and reads the Form.

Panade
  • 309
  • 3
  • 12