Search code examples
jqueryregexjquery-validation-engine

Jquery ValidationEngine Swap Rule Based on Dropdown Selection


I have a dropdown box that contains contact "types" (Phone, Email, Postal Code, etc).

Depending on the selection, a second textbox needs to have different validation rules applied.

How can I dynamically add/remove jquery.validationEngine rules to/from the textbox? Here's the easy part.

$(function () {
    $("[id$=inputType]").change(function () {
        switch ($(this).val()) {
            case "Phone":
            case "Mobile":
                break;
            case "Email":
                break;
            case "PostalCode":
                break;
            default:
                break;
        }
    });
});

Solution

  • I think you should not use such logic here.
    According official documentation, you can use a function to validate your inputs using funcCall[methodName] directive.

    So you can use code some thing like this:

    <input value="" class="validate[required,funcCall[checkHELLO]]" type="inputType" id="selectTypes" name="selectTypes" />
    
    function checkHELLO(field, rules, i, options){
        switch ($(this).val()) {
            case "Phone":
            case "Mobile":
                break;
            case "Email":
                break;
            case "PostalCode":
                break;
            default:
                return options.allrules.validate2fields.alertText;
         }
      }
    }