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

4
  • 1
    What am I missing here. You have regexpValidar with no value and regexNIT that does not get used. Commented Dec 7, 2018 at 17:23
  • Use 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 JS Commented Dec 7, 2018 at 17:23
  • You probably mean A-Z range. Commented Dec 7, 2018 at 17:24
  • Its returning true because it is matching the single character 6, not because of ! Commented Dec 7, 2018 at 17:26

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@JohanWentholt Thank you, you are correct. Updated answer.
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])+$"

1 Comment

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.
0

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

var regexCompilado = new RegExp(regexNIT);

1 Comment

Thanks Alexandros. Same as @TecBrat had commented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.