Declare an enum type string, like this:
public enum SEXO
{
M = "Masculino",
F = "Feminino"
}
How do I get an enum similar to the one above. This way give error:
Can not implicitly convert type 'string' to 'int'
Declare an enum type string, like this:
public enum SEXO
{
M = "Masculino",
F = "Feminino"
}
How do I get an enum similar to the one above. This way give error:
Can not implicitly convert type 'string' to 'int'
You can not, at least not directly from the way you're trying to ask the question.
You can achieve a similar effect using extensions
, see example
public static string GetStringValue(this Enum value)
{
var type = value.GetType();
var fieldInfo = type.GetField(value.ToString());
var attributes = fieldInfo.GetCustomAttributes(
typeof(StringValueAttribute), false) as StringValueAttribute[];
var stringvalue = attributes != null && attributes.Length > 0 ? attributes[0].StringValue : value.ToString();
return stringvalue;
}
public class StringValueAttribute : Attribute
{
public string StringValue { get; protected set; }
public StringValueAttribute(string value)
{
StringValue = value;
}
}
enum declaration
public enum Sexo
{
[StringValue("Masculino")]
M = 1,
[StringValue("Feminino")]
F = 2
}
Use
string descrEnum = Sexo.M.GetStringValue();
I vaguely remembered that this code had come out of here from SOpt, now I hardly found in this answer .
I have a form and I need this field
%pre%In case it is the phone, it will display the following error messages: If the user types less than 8 digits, an error message appears, a %code% .
If the user types "-" which is an invalid character, an alert with = %code%
And if the number is valid (8 digits) nothing appears.
My code:
%pre%It's not working.
The reference in the HTML to the javascript has to be by onKeyup if possible, as the javascript has to check after the user types.
Sincerely,
Gabriel
I suggest using the validation supported by the browser itself. This only works if the browser supports HTML5
To use something more complete or different you can continue to use the onchange event with this automatic validation:
You can do this:
Remembering that it is more practical to limit size by html using maxLength, and using onblur prevents it from showing the message at all times when typing.
To complement, I'll leave a more complete version of @GabrielRodrigues' answer.
I made using the jQuery Mask Plugin , I believe it increases the usability of the field since it already comes formatted, in addition it it also "prohibits" any letters or any non-digit characters from being typed.
I got it! I used the answer from @Bruno Costa and Js from @Gabriel Rodrigues. It just looks like this:
%pre%