jQuery验证 - 检查字符串是否具有unicode或空格字符不起作用

时间:2013-08-14 02:07:06

标签: javascript jquery jquery-validate

请看我的代码,checkUser功能不起作用。我认为value.match()函数有错误:

<form id="register_form" method="post" action="">
    <fieldset>
        <legend>Info</legend>

        <div>
            <label>Username</label>
            <input type="text" name="username" id="username"/>
            <input type="submit" id="btnAccess" class="button" value="Submit" name="btnAccess"/>
    </fieldset>
</form>
$(document).ready(function() {
    var validator = $('#register_form').validate({
        rules: {
            username: {
                required: true,
                minlength: 4,
                checkUsername: true
            }
        },
        messages: {
            username: {
                required: "No blank",
                minlength: "Enter atleast 4 words"
            }
        }
    });

    $.validator.addMethod('checkUsername', function(value, element) {
        return this.optional(element) || !/\s/g.test(value) || !value.match(/^ÀÁÂÃÈÉÊÌÍÒÓÔÕÙ+/);
    },  "No space or unicode character");
});

您可以在此处查看http://jsfiddle.net/nambatre/rEpqr/

1 个答案:

答案 0 :(得分:1)

尝试

jQuery(function ($) {
    var validator = $('#register_form').validate({
        rules: {
            username: {
                required: true,
                minlength: 4,
                checkUsername: true
            }
        },
        messages: {
            username: {
                required: "No blank",
                minlength: "Enter atleast 4 words"
            }
        }
    });

    $.validator.addMethod('checkUsername', function(value, element) {
        return this.optional(element) || (!/\s/g.test(value) && !/[ÀÁÂÃÈÉÊÌÍÒÓÔÕÙ]/.test(value));
    },  "No space or unicode character");;
});

演示:Fiddle