Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow alphanumeric with spanish regex in javascript?

Hi I tried the following code in javascript,

if(/[^0-9a-zñáéíóúü]/i.test(string))
{
    alert("oK WITH THE INPUT");
}
else
{
    alert("error");
}

But I am getting a result OK , even I entered the @ characters in my string. I dont why.

My aim is to allow alphanumeric(English and spanish).

Please help me in this.

Thanks in advance.

like image 267
svk Avatar asked Jun 21 '13 23:06

svk


People also ask

Does regex work for other languages?

Regular expressions are easy to learn, self-containing (its syntax is rarely changed or updated), very powerful and language agnostic, since they work for all natural languages and with majority of programming languages.

How do you denote special characters in regex?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).

What is a zA Z in JavaScript?

Description. [a-zA-Z] matches any character from lowercase a through uppercase Z.

Does JavaScript support regex?

Using regular expressions in JavaScript. Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.


1 Answers

if(/^[0-9a-zñáéíóúü]+$/i.test(string))
{
    alert("oK WITH THE INPUT");
}
else
{
    alert("error");
}
  • Make the regex match more than one character with the + quantifier
  • Make it match the entire input with ^ and $ anchors
  • Don't negate the character class (remove the ^)
like image 168
Matt Ball Avatar answered Sep 27 '22 17:09

Matt Ball