I have this input
[DataType(DataType.Password)]
public int Pin
<input asp-for="Pin" Pattern="[1-9]+" />
yet it still lets me type letters
I have this input
[DataType(DataType.Password)]
public int Pin
<input asp-for="Pin" Pattern="[1-9]+" />
yet it still lets me type letters
I personally would just use the password input field and then use JavaScript to block all non numerical input from being added.
Here's an example:
document
.getElementById("numeric_input")
.addEventListener("keypress", function(evt) {
if (evt.which < 48 || evt.which > 57) {
evt.preventDefault();
}
});
The pattern attribute validates the input on form submission, but doesn't enforce it while typing.
The basic HTML form validation features will cause this to produce a default error message if you try to submit the form with either no valid filled in, or a value that does not match the
pattern.