I am trying validate a data to set in an input. I want to use de Validation HTML5.
For example, I want to check if "aaaa" is a valid data for an number input. I wanted use willValidate
or validity
but I can not set an invalid value, the console shows (Chrome):
The specified value "aaaa" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+.\d+|.\d+)([eE][-+]?\d+)?
I have tried catch the error but it is not an Exception. I retrieve the input value and it is empty after the error.
var value = "aaaa";
try {
document.getElementById("input").value = value; // Shows a warn
} catch(err) {
console.log(err.message); // Nothing catching
}
console.log( document.getElementById("input").value ); // It is empty
MORE EXPLAINED:
I want to check if a value to setted is valid in the input type. I thought set the invalid value in the input and check willValidate or validity but the browser shows a warn and the input value is empty. The problem is that my frontend set the inputs values via javascript but the user pass the values. When an error occurs I need show the error to the user and not put only and input empty..
UPDATED:
I know that the warn is not an Exception. I want know shows the error when set an invalid input value in any type.
var value = "aaaa";
document.getElementById("input").value = value;
if (document.getElementById("input").value == null || document.getElementById("input").value == "") {
alert("Invalid value");
}