Inputmask by the model

Asked

Viewed 545 times

3

Hello, I’m using the plugin jQuery - maskedInput, where I take the id of my input and make the mask:

$("#CPF").mask("999.999.999-99");

is working, only that would like to use this mask through the model, without the need for a js external

    [RegularExpression(@"[0-9.-]{14}", ErrorMessage = "CPF deve conter apenas números")]
    public string CPF { get; set; }

thank you

  • @Marconi there I’m only treating the Pattern, I wanted the mask in the input,

  • You want to use mask by Regularexpression?

  • 1

    @Marconi , no, I have the file js 'jquery.maskedinput.js' I say I want to use the mask in the model, so it is direct, I do not need to keep taking the #id of my inputs, got?

  • I get it, if you can ride it will be cool.

1 answer

4


Actually you want to validate the CPF on Controller, if I understand correctly.

Implement the CPF validation attribute:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
sealed public class CpfAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null) return null;

        int soma = 0, resto = 0;
        string digito;
        int[] multiplicador1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };
        int[] multiplicador2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

        string CPF = value.ToString().Replace(".", "").Replace("-", "");

        if (CPF.Length != 11)
            return new ValidationResult("CPF Inválido.");

        if (Convert.ToUInt64(CPF) % 11111111111 == 0)
            return new ValidationResult("CPF Inválido.");

        string tempCpf = CPF.Substring(0, 9);

        for (int i = 0; i < 9; i++)
            soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];

        resto = soma % 11;
        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = resto.ToString();
        tempCpf = tempCpf + digito;
        soma = 0;

        for (int i = 0; i < 10; i++)
            soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];

        resto = soma % 11;

        if (resto < 2)
            resto = 0;
        else
            resto = 11 - resto;

        digito = digito + resto.ToString();

        if (CPF.EndsWith(digito))
            return null;
        else
            return new ValidationResult("CPF Inválido.");
    }

    public override string FormatErrorMessage(string name)
    {
        return name;
    }
}

Then decorate the property with it:

[RegularExpression(@"[0-9.-]{14}", ErrorMessage = "CPF deve conter apenas números")]
[Cpf]
public string CPF { get; set; }

About not using JS in View, It’s something I’ve been trying to do for a while. I asked this question some time ago, but the answer is not 100% of what I wanted.

  • From what I understand it seems that he wants to leave the mask in the CPF property of the model. See in the comments below the question.

  • this CPF validation attribute, where do I put it? I have to create a class?

  • @Marconi So, that’s why I put the question I asked in the old days. I also want to know in a more elegant way how to do. I haven’t been able to.

  • @Furlan Yes. Create the class within a project directory called Attributes, preferably.

  • @Ciganomorrisonmendez se consegue fazer isso: http://www.codeproject.com/Tips/642477/Input-Masking-in-MVC-using-Data-Annotation . Would be the answer to problems kk

  • @Furlan I’ve tried. It doesn’t work.

  • @Ciganomorrisonmendez ah, so I will use the same jQuery, picking up the #id of each element and good, thanks again :)

  • I understood Gypsy, I didn’t even know what I had to do. Very cool the question.

  • @Marconi It is capable that I try this solution of the Code Project again any day of these, then update the two answers.

  • cool, even I’ll give a study to know how it works.

  • The example of the controller was very good, it would only change the regex to something like ^\d+{3}\.\d+{3}\.\d+{3}\-\d{2}$

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.