Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Members.

I need a slight help from you. I have a "RequiredIf" which is comparing a value from "EnumDropdown" and a "string" field but the code is not working in Jquery validation. Can anyone please suggest a way to do that.Below are the code snippets.

ViewModel :

C#
[RequiredIf("ValidationOption", CustomerParameter.Phone, ErrorMessageResourceType = typeof(Customer_cshtml), ErrorMessageResourceName = "PhoneRequired")]
        public string Phoner { get; set; }


[RequiredIf("ValidationOption", CustomerParameter.Account, ErrorMessageResourceType = typeof(Customer_cshtml), ErrorMessageResourceName = "CustomerRequired")]
       
        public string Account{ get; set; }

public CustomerParameter ValidationOption { get; set; }


JQUERY:

C#
$.validator.addMethod('requiredif',
    function (value, element, parameters) {
        var id = '#' + parameters['dependentproperty'];

        
        var targetvalue = parameters['targetvalue'];
        targetvalue =
          (targetvalue == null ? '' : targetvalue).toString();

        
        var control = $(id);
        var controltype = control.attr('type'); --->//This line is getting undefined for EnumDropdownList property
        var actualvalue ;
        if (controltype === 'checkbox')
        {
            actualvalue = control.is(':checked').toString();
        } else if (controltype === 'radio') {
            actualvalue = $('input[name='+parameters['dependentproperty']+']:checked').val()
        } else {
            actualvalue = control.val();
        }

       
            if (targetvalue === actualvalue)--->///////In this comparison area targetvalue if fetching the name of the property like Phone or Account and the actual value is fetching the value of selected enums value like 0,1 etc so the rules are not getting added.
            return $.validator.methods.required.call(
              this, value, element, parameters);

        return true;
    }
);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900