0

大家好,我正在尝试验证我的表单,其中一个字段是 URL,到目前为止我的规则是:

 rules: {
                url: { required: true, url: true},
                name: { required: true },
                provider: { required: true }
            }

我还想在规则中使用类似 url:has(http) 的东西来检查协议是否存在。我如何实现这一目标?

4

1 回答 1

1

尝试

$.validator.addMethod("checkURL", function(value, element)
{
    var return_val = null;
    var message = null;
    var reg = /http(s?):\/\/www/;

    if (!reg.test($(element).val()))
    {
        return_val = false;
        message = "false message";
    }
    else
    {
        return_val = true;
        message = "";
    }
    jQuery.validator.messages.checkURL = message;
    return return_val;
}, jQuery.validator.messages.checkURL);

用它作为

rules:
{
    url: { required: true, checkURL: true},
    name: { required: true },
    provider: { required: true }
}
于 2013-03-18T10:34:52.163 回答