jquery validation - optional field
<p>In a registration form middleName field is optional. When the users enters his/her middleName, then it should validate that field.</p>

<p>I have used the following code to achieve the above scenario, but it is not working.</p>

<pre><code>$("#middleName").rules("add", {checkName: true, required: false, messages: {checkName: "Please enter a valid middle name"} });

jQuery.validator.addMethod("checkName",
        function(value, element) {
            var regExp = new RegExp(/^[a-zA-Z0-9\s|\,|\.|\-|\']+$/);
            return regExp.test(value);
        },
        "Please enter a valid name."
);
</code></pre>

<p>Can anyone suggest me to achieve the above scenario.</p>
