place Custom EN Message in required field

Asked

Viewed 292 times

2

Assigns Required="true" in a password field, but the message is in English, as seen in the image below:

inserir a descrição da imagem aqui

I wanted to personalize this message by leaving it in PT-BR

    <div class="form-group">
        @Html.LabelFor(model => model.Senha, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Senha, new { htmlAttributes = new { @class = "form-control senha", id = "Senha", required="true" } })
            @Html.ValidationMessageFor(model => model.Senha, "", new { @class = "text-danger" })
        </div>
    </div>

Obs: I didn’t do no anottation, why the password attribute is in the person class, which is inherited to the user and client class, where the user will have a mandatory password and the client will not have a password.

1 answer

2

This error is related to the required Html5 attribute. This is the default message. You can change it via javascript:

<form id="form2">
    <input type="text" required />
    <input type="submit">
</form>


$('#form2 input[type=text]').on('change invalid', function() {
    var textfield = $(this).get(0);

    // 'setCustomValidity not only sets the message, but also marks
    // the field as invalid. In order to see whether the field really is
    // invalid, we have to remove the message first
    textfield.setCustomValidity('');

    if (!textfield.validity.valid) {
      textfield.setCustomValidity('Campo Obrigatório');  
    }
});

http://jsfiddle.net/zpkKv/665/

Browser other questions tagged

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