I'm trying to validate the value of an input text field with the following code:
function onBlurTexto(value) {
var regexNIT = "([a-zA-z]|[0-9]|[&#,@.ÑñáéíóúÁÉÍÓÚ\|\s])";
regexCompilado = new RegExp(regexNIT);
if (!(regexCompilado.test(value))) {
alert("Wrong character in text :(");
return false;
} else {
return true;
}
}
But when i enter this text:
!65a
the function returns true (as you can see, the "!" character does not exist in the regular expression)
I'm not an expert in regular expressions, so i think i am missing something in the building of this reg.exp.
How can i put this regular expression to work?
Thanks in advance.
EDIT
i am so sorry ... i should remove the references to the variable "regexpValidar" before posting the issue. I modified the sample. Thanks @TecBrat
regexpValidarwith no value andregexNITthat does not get used.if (/^[a-zA-Z0-9&#,@.ÑñáéíóúÁÉÍÓÚ|\s]+$/.test(value)) {...}. Note that[A-z]matches more than just letters. If empty string is allowed, replace+with*. If|is used as an OR operator, just remove it from the character class (since it is treated as a literal pipe char). Note that in a string literal, all backslashes must be doubled to denote literal backslashes."\s"="s"in JSA-Zrange.