0

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

Javi
  • 19
  • 1
  • 7
  • 1
    What am I missing here. You have `regexpValidar` with no value and `regexNIT ` that does not get used. – TecBrat Dec 07 '18 at 17:23
  • Use `if (/^[a-zA-Z0-9,@.ÑñáéíóúÁÉÍÓÚ|\s]+$/.test(value)) {...}`. Note that [`[A-z]` matches more than just letters](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926). 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 JS – Wiktor Stribiżew Dec 07 '18 at 17:23
  • You probably mean `A-Z` range. –  Dec 07 '18 at 17:24
  • Its returning true because it is matching the single character 6, not because of ! –  Dec 07 '18 at 17:26

3 Answers3

2

You should provide the start (^) and end ($) flags to your regex. Now you are matching 65a since you have alternate sets.

This should work /^([a-zA-z]|[0-9]|[&#,@.ÑñáéíóúÁÉÍÓÚ\|\s])+$/g

Demo: https://regex101.com/r/zo2MpN/3

Alex G
  • 1,897
  • 2
  • 10
  • 15
  • This matches only one character. Even if a string `'65a'` is provided it wouldn't match. I don't thing that's the intention of OP. Otherwise good answer. – 3limin4t0r Dec 07 '18 at 17:39
  • @JohanWentholt Thank you, you are correct. Updated answer. – Alex G Dec 07 '18 at 17:42
1

RegExp.test looks for a match in the string, it doesn't verify that the whole string matches the regex. In order to do the latter, you need to add start and end anchors to your regex (i.e. '^' at the start and '$' at the end, so you have "^your regex here$").

I also just noticed that your regex is currently matching only one character. You probably want to add a '+' after the parens so that it matches one or more:

"^([a-zA-z]|[0-9]|[&#,@.ÑñáéíóúÁÉÍÓÚ\|\s])+$"
manveti
  • 1,691
  • 2
  • 13
  • 16
  • Thank you @everybody for your kind answers !!!! I was sure that something was missing ( the start (^) and end ($) flags ). Now i put this regexp to work. – Javi Dec 07 '18 at 18:00
0

This is wrong. the variable you use doesn't has anything. Try this instead.

var regexCompilado = new RegExp(regexNIT);
Alex Bntz
  • 124
  • 9