1

I have this javascript function that block special characters...

function validaTexto(texto)
{
    !(/^[A-zÑñ0-9]*$/i).test(texto.value) ? texto.value = texto.value.replace(/[^A-zÑñ0-9]/ig, '') : null;
}

The problem is that this function doesn't allow me to type blank spaces... how can I customize this function to allow me some other things, such as blank spaces, "," , "." , ";" and so on?

Thanks!!

3 Answers 3

2

change the regex to this:

!(/[^A-zÑñ0-9 ,\.;]*$/i)

also, the function is quite redundant in that it checks the string twice, basically saying "Does the string contain any of these characters? Yes? Ok, so search the string for these same characters and remove them. Just change it to this:

function validaTexto(texto) {
    texto.value.replace(/[^a-zñ0-9 ,\.;]/ig, '');
}
Sign up to request clarification or add additional context in comments.

Comments

1
function validaTexto(texto) {
    texto.value.replace(/[^A-z0-9 ,\.;]/ig, '');
}

Referenes (with examples):

1 Comment

there's an error in the first regex. it's checking whether the entire string consists of the allowable characters, and if it does, then remove the ones which aren't allowable. also, the special "ñ" character isn't checked for here.
0

Read this post:

Regular Expression: Allow letters, numbers, and spaces (with at least one letter or number)

With a bit of effort, you should be able to modify your regex to do what you need.

Comments

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.