添加选择表单验证器

时间:2013-06-04 12:13:20

标签: javascript html

我使用此代码验证输入文本和输入复选框。但我想添加选择框验证所需的功能。我希望用户必须选择一个字符串并选择框填充。

required:{
            set:function(val) {
                //Check the type of the input
                switch(this.tagName) {
                    case 'INPUT':
                        if($(this).attr('type') == 'text') {
                            val = $.trim(val);
                            if(val === '') {
                                return false;
                            }else {
                                return true;
                            }
                        }else if($(this).attr('type') == 'checkbox') {
                            return $(this).is(':checked') ? true : false;
                        }
                        break;  
                    default:
                        return false;
                        break;
                }
            },
            message:'This field is required.'
        },

但我不知道添加代码的内容和位置。

1 个答案:

答案 0 :(得分:0)

else if($(this).attr('type') == 'checkbox') {
    return $(this).is(':checked') ? true : false;
}
else if($(this).attr('select') == '') {
    if(val === '') {
        return false;
    }else {
        return true;
    }
}