Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加选择表单验证程序_Javascript_Html - Fatal编程技术网

Javascript 添加选择表单验证程序

Javascript 添加选择表单验证程序,javascript,html,Javascript,Html,我使用此代码验证输入文本和输入复选框。但我想添加选择框验证所需的功能。我希望用户必须选择一个字符串并选择框填充 required:{ set:function(val) { //Check the type of the input switch(this.tagName) { case 'INPUT': if($(thi

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

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.'
        },

但是我不知道在什么地方添加代码。

如果你写了代码,那么你就知道在哪里添加代码如果在else之后再写另一个else如果
else if($(this).attr('type') == 'checkbox') {
    return $(this).is(':checked') ? true : false;
}
else if($(this).attr('select') == '') {
    if(val === '') {
        return false;
    }else {
        return true;
    }
}