5

I have a enum in my C# code and i want to get names from enum in my jQuery validate rules.

Enum:

public enum EnumItemField
{
    Zero = 0,
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
} 

Validate:

function updateFieldStatus() {
}

$(document).ready(function () {
$("#IntegrationService").validate({
    rules: {
        //Set range of Start
        "config.EnumFormat[Zero].Start": {
            required: true,
            digits: true,
            range: [1, 200]
        },
        "config.EnumFormat[One].Start": {
            required: true,
            digits: true,        
            range: [1, 200]
        },
    },
    submitHandler: function () {
        MsgBoxService.show({
            //setId: "saveload",
            //objectId: "asrun"
        });
    },
}); 

This really works, but I want make something like this:

"config.EnumFormat[" + item + "].Start": {
    required: true,
    digits: true,
    range: [1, 200]
}

In C# I can get names in this way:

foreach (var item in Enum.GetNames(typeof(EnumItemField)))

How can I make it in javascript? Thanks for advice!

5
  • Hi str_str, kindly check out my answer and let me know if you need any more help! Commented Dec 29, 2015 at 14:56
  • I have no idea why your answer don't work Commented Dec 29, 2015 at 15:01
  • var is not identified, maybe I do something wrong? Commented Dec 29, 2015 at 15:04
  • Please upload a screenshot! This is crazy. Doesn't look like a JavaScript error. Are you sure this is JavaScript you need? Commented Dec 29, 2015 at 15:04
  • Magic, problem fixed. Thank you for help! Commented Dec 29, 2015 at 15:08

1 Answer 1

6

You can make the same thing as a JavaScript Object.

EnumItemField = {
    "Zero": 0,
    "One": 1,
    "Two": 2,
    "Three": 3,
    "Four": 4,
    "Five": 5
}

Please don't forget to remove the last , comma.

You can use the values like:

EnumItemField.Zero     // 0
EnumItemField["Zero"]  // 0

Iteration? No problem:

for (var item in EnumItemField) {
  item;                // Zero
  EnumItemField[item]; // 0
}
Sign up to request clarification or add additional context in comments.

1 Comment

@MatíasFidemraizer Yeah, sure. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.