bboks.net™

jQuery Validate - custom regex 본문

Web/jQuery

jQuery Validate - custom regex

bboks.net 2013. 3. 21. 17:29

Validator

jQuery.validator.addMethod(          

"regex",

    function(value, element, regexp) {

        if (regexp.constructor != RegExp)

            regexp = new RegExp(regexp);

        else if (regexp.global)

            regexp.lastIndex = 0;

        return this.optional(element) || regexp.test(value);

}, "검증 실패 메세지");


Validate

$("#form_id").validate({
    rules : {
        name : { required : true },
        code : {required : false, regex : "^[cC][a-zA-Z0-9]{5}$"}
    },
    submitHandler : submitHandler
});



[출처] JQuery validate: How to add a rule for regular expression validation?