1

请查看我的代码,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/

4

1 回答 1

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");;
});

演示:小提琴

于 2013-08-14T02:21:34.313 回答