Reputation: 229
I have a problem with the validation of a number type field where the identification of a person must be placed, my data annotations is this
[Required(AllowEmptyStrings = false, ErrorMessage = "El RUT es Obligatorio")]
[RegularExpression("(^[0-9]+$)", ErrorMessage = "Solo se Permiten Números")]
[StringLength(10, ErrorMessage = "El RUT solo permite 10 dígitos")]
This is my view of Register
<div class="form-group col-md-6">
<label>RUT Paciente:</label>
<input asp-for="MODEL_PACIENTE.PAC_RUT" type="number" placeholder="RUT" class="form-control" autofocus />
<span asp-validation-for="MODEL_PACIENTE.PAC_RUT" class="text-danger"></span>
</div>
The error is when I put the letter e, it is the only letter that allows me to enter both uppercase and lowercase.
How can I correctly validate this field?
Upvotes: 1
Views: 800
Reputation: 229
For the problem of the letter 'e' , i use a script en el event onkeypress="return Solo Numeros(event); of my input
<div class="form-group col-md-6">
<label>RUT Paciente:</label>
<input asp-for="MODEL_PACIENTE.PAC_RUT" type="number" placeholder="RUT" onkeypress="return SoloNumeros(event);" class="form-control" autofocus />
<span asp-validation-for="MODEL_PACIENTE.PAC_RUT" class="text-danger"></span>
</div>
I put here the script code
function SoloNumeros(e) {
var keynum = window.event ? window.event.keyCode : e.which;
if ((keynum == 8 || keynum == 48))
return true;
if (keynum <= 47 || keynum >= 58) return false;
return /\d/.test(String.fromCharCode(keynum));
}
Upvotes: 0
Reputation: 8935
See the following post: Why does the html input with type “number” allow the letter 'e' to be entered in the field?
This answer why your <input>
accept the 'e' letter. But, it goes without saying, the MVC validation reject it.
The post does not contain data model definition. Therefore, I am improvising...
I called data model class as DataModel
:
public class DataModel
{
[Required(AllowEmptyStrings = false, ErrorMessage = "El RUT es Obligatorio")]
[RegularExpression("(^[0-9]+$)", ErrorMessage = "Solo se Permiten Números")]
[StringLength(10, ErrorMessage = "El RUT solo permite 10 dígitos")]
public string PAC_RUT { get; set; }
}
And used HtmlHelper
to generate the HTML:
@model DataModel
<div class="form-group">
@Html.LabelFor(m => m.PAC_RUT)
<p>@Html.TextBoxFor(m => m.PAC_RUT, htmlAttributes: new { type="number", autofocus = "autofocus" })</p>
@Html.ValidationMessageFor(m => m.PAC_RUT)
</div>
...
And this is the .cshtml
compiled to the HTML. So, as you can see when using the HtmlHelper
, the generated HTML that including set of additional attributes allowing to enter only 10 digits:
<div class="form-group">
<label for="PAC_RUT">PAC_RUT</label>
<p><input class="input-validation-error" data-val="true" data-val-length="El RUT solo permite 10 dígitos" data-val-length-max="10" data-val-regex="Solo se Permiten Números" data-val-regex-pattern="(^[0-9]+$)" data-val-required="El RUT es Obligatorio" id="PAC_RUT" name="PAC_RUT" type="number" value="" /></p>
<span class="field-validation-error" data-valmsg-for="PAC_RUT" data-valmsg-replace="true">El RUT es Obligatorio</span>
</div>
Upvotes: 1