0

I have to validate a series of inputs on my app. The user can enter any character cause the app has multi-language support.

So i need block only numbers and special characters but not the accentuated ones...

Can you guys help?

My code:

var textInputs,
AllowRegex  = [A-Za-z\u00c0-\u017e ];

        textInputs = document.querySelectorAll('input[type=text]');


        for(i = 0; i < textInputs.length; i++){
            if ( !AllowRegex.test(textInputs[i].value) ){
                alert(textInputs[i].value);
            } else {
                $obj.list[i] = textInputs[i].value;
            }
        }

        console.log($obj.list);
Rafael de Castro
  • 888
  • 4
  • 16
  • 37

1 Answers1

1

You can use unicode ranges to include all the special characters you need.

\[A-Za-z\u00c0-\u017e ]\

will cover most of them, but possible not all of them. You can look up specific characters codes you need on this website. As you can see in the example above you just precede the character code with \u to state that it's using unicode.

It should be noted that this will not work with all implementations of regex, but does work with JavaScript

Luke K
  • 865
  • 6
  • 13
  • I edited my question with my code, but the regex you gave didn't work. Fire an reference error "A is not defined" – Rafael de Castro Nov 23 '16 at 22:37
  • 1
    Stupid question, but did you just copy and paste it in as a string. With regex you need to surround it with backslashes or put it as a string argument to myRegex = new RegExp("myRegex"). I've edited the answer to include the former, because it sounds like however you put it in JavaScript thinks you mean a variable – Luke K Nov 23 '16 at 22:39
  • helped, but not entirely. If I put for example a ? it blocks. Ok! But if I insert a space after, breaks. – Rafael de Castro Nov 23 '16 at 23:09