简体   繁体   English

在国家/地区代码下拉列表中添加所需的验证[intl-tel-input jquery plugin]

[英]Add required validation on country code drop down [ intl-tel-input jquery plugin ]

intl-tel-input plugin link 国际电话输入插件链接

I am using this plugin and i want to add required validation on country code drop down but this plugin don't let me do it. 我正在使用此插件,我想在国家/地区代码下拉列表中添加所需的验证,但此插件不允许我这样做。

i tried it but failed. 我尝试了但是失败了。

First i add selected class on drop down click event 首先,我在下拉点击事件中添加选定的课程

$(document).on('click', '.country', function () {
    $(".flag-dropdown").find('.selected-flag').addClass('selected');
});

then i checked if the div doesn't have selected class then add error class in input field container div 然后我检查了div是否没有选择类别,然后在输入字段容器div中添加了错误类别

But it's not working 但这没用

$("input[name='mobile']").keypress(function () {
    var isSelected = $('.flag-dropdown').find('.selected-flag').hasClass('selected');
    if (isSelected == false) {
        $('.has-feedback').removeClass('has-success');
        $('.has-feedback').addClass('has-error'); 
    }
});

I am using bootstrap validation also 我也在使用引导验证

      mobile: {
                validators: {
                    stringLength: {min: 8, message: " "},
                    notEmpty: {message: " "},
                    regexp: {regexp: /^\+?[\d-]+$/, message: " "}
                }
              }

Any help will be appreciated.! 任何帮助将不胜感激。!

I also used this plugin and stuck in same issue and solved by these steps 我也使用了这个插件,并陷入了同样的问题,并通过以下步骤解决了

1 : Add callback function to your bootstrap validation 1 :将回调函数添加到您的引导验证中

          mobile: {
                    validators: {
                        notEmpty: {message: " "},
                        regexp: {regexp: /^\+?[\d ]+$/, message: " "},
                        callback: {
                            message: '',
                            callback: function (value, validator, $field) {
                                if(!$('div.flag-dropdown').find('.country').hasClass('active')){
                                    return false;
                                }else{
                                    return true;
                                }
                            }
                        },
                    }
                },

2 : Change you click event by this code 2 :通过此代码更改点击事件

$(document).on('click', '.country', function () {
    var selectCode = $('ul.country-list').find('.active').attr('data-dial-code');
    if($.type(selectCode ) === "string") {
        $('#contact_mobile').val('+' + selectCode);
    }else{
        $('#contact_mobile').val('');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM