开发者

jquery validator add method

开发者 https://www.devze.com 2023-03-03 19:29 出处:网络
I am having an issue getting this to work. On our form we validate zip code (postal code) length based on the country you select. Australia and Hungary have min length of 4 characters, Bahrain must ha

I am having an issue getting this to work. On our form we validate zip code (postal code) length based on the country you select. Australia and Hungary have min length of 4 characters, Bahrain must have at least 3 and all other countries must have at least 5. So I wrote a rule for:

Australia

$.validator.addMethod("AusZip", function(value, element) {
    var isAus = $("#UpdateCountry").val() === "AUS";
    if ( isAus && value.length < 4 ) {
        return false;
    } else return true;
}, "Australian Zip Code Must Be at least 4 Digits");

Hungary

$.validator.addMethod("HunZip", function(value, element) {
    var isHun = $("#UpdateCountry").val() === "HUN";
    if ( isHun && value.length < 4 ) {
        return false;
    } else return true;
}, "Hungarian Zip Code Must Be at least 4 Digits");

and Bahrain

$.validator.addMethod("BHRZip", function(value, element) {
    var isBHR = $("#UpdateCountry").val() === "BHR";
    if ( isBHR && value.length < 3 ) {
        return false;
} else return true;
}, "Bahrain Zip Code Must Be at least 3 Digits");

All other countries specified here, and this is where my problem is

$.validator.addMethod("RegZip", function(value, element) {
    var notAus = $("#UpdateCountry").val() != "AUS";
    var notBHR = $("#UpdateCountry").val() != "BHR";
    var notHun = $("#UpdateCountry").val() != "HUN";
    if ( notAus && notBHR && notHun && value.length < 5 ) {
        return false;
    } else return true;
}, "Zip Code Must Be at least 5 Digits");

When I add the rule for "RegZip" even if you select Australia or Hungary and have a 4 digit zip, you get the error - same if you select Hungary and have a 3 digit zip. When I remove "RegZip" you only get an error with Australia/Hungary if it's less than 4 digits and error on Hungary if less than 3. The pro开发者_开发知识库blem is that all other countries have no requirement. What am I missing?

thx!

EDIT - added "UpdateCountry" as the element ID. works. sorry for confusion - my bad!


A better way to do this would be something like so:

 var zips = {
  'AUS': { min: 4 },
  'HUN': { min: 4 },
  'BHR': { min: 3 }

};
var zipOther = { min: 5 };

$.validator.addMethod('zip', function(value, element) {
  return value.length >= (zips[$('#UpdateCountry').val()] || zipOther)['min'];
}, 'Zip code not the correct length');


With the code you provided you are only validating the three countries in the RegZip function as everything else returns true. You should have some other generic zip validation being calling in the else clause.


I would get rid of all your checks on $("#Country").val() and simple change the class name on the input on change of Country select

EDIT

first add class "RegZip' to the input you want to validate as starting point

then add validator methods

$.validator.addMethod("AusZip", function(value, element) {
    if (value.length < 4 ) {
        return false;
    } else return true;
}, "Australian Zip Code Must Be at least 4 Digits");

//Hungary

$.validator.addMethod("HunZip", function(value, element) {
    if (value.length < 4 ) {
        return false;
    } else return true;
}, "Hungarian Zip Code Must Be at least 4 Digits");

//and Bahrain

$.validator.addMethod("BHRZip", function(value, element) {
    if (value.length < 3 ) {
        return false;
    } else return true;
}, "Bahrain Zip Code Must Be at least 3 Digits");

//All other countries specified here, and this is where my problem is

$.validator.addMethod("RegZip", function(value, element) {
    if ( notAus && notBHR && notHun && value.length < 5 ) {
        return false;
    } else return true;
}, "Zip Code Must Be at least 5 Digits");

add change event to dropdown list

$('#dropdown_menu_selector').onchange(function(){
    if (this.value === "AUS"){
        $('#input_to_validate').removeClass().addClass('AUSZip')
    }
    //etc etc..
});

validate the form

   $('#formid').validate();
0

精彩评论

暂无评论...
验证码 换一张
取 消