Old-New Probleam: JQuery Validator and Optional Fields

I have an optional field (say “text1″) which may either be blank or only alpha-numeric:

jQuery.validator.addMethod("onlyAlphaNumeric",
        function(value, element) {
            var regExp = new RegExp(/^[a-zA-Z0-9]+$  /);
            return ((this.optional(element)) || regExp.test(value));
        }
    , "Only aplaha-numeric characters allowed");

$  ("#student-search-form").validate({
    rules : {
        text1 : {
            optional : true,
            onlyAlphaNumeric: "Only a-n allowed"
        }
    },
    messages: {
        text : {
            acceptOnly: " Only alpha-numeric characters allowed"
        }
    }
});

The problem is no validation happens, so if user enters “!&^%(*” in ‘text1′, the form gets submitted, no error checks.

Can somebody please tell me what am I doing wrong?
Thank you.

newest questions tagged jquery – Stack Overflow

About Admin