0

I want to prevent users inputing special characters ()&<>[]{}% in the many input and text areas of our website. Which is the best way ? Users use IE8, Firefox and Chrome. The name and id in the input and textarea is dynamic. I have no control over it.

So basically, I just want a function and use the onclick event to call it. I thought :input selector would do the work. Thanks.

Please take a look at JSFIDDLE

function spCheck () {var str = $('textarea').val(); if(/^[a-zA-Z0-9- ]*$/.test(str) == false) { alert('You have entered illegal characterss. Please remove them and try again.'); }} 
2
  • What is your question exactly? Your code seems to function as expected. Commented Nov 15, 2013 at 16:21
  • Sorry, if I am not clear. When a user enters illegal characters into the textarea or input box, the alert needs to pop up. I have no control of adding classes or ids. My JS Fiddle, works only on the first text area. Commented Nov 19, 2013 at 10:22

1 Answer 1

1

Add class notSpecilChar to elements you want to check and on blur alert or keyup event.

$('.notSpecilChar').blur(function () { if (/^[a-zA-Z0-9- ]*$/.test(this.value) === false) { alert('You have entered illegal characterss. Please remove them and try again.'); } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi! Tushar, Thanks for the reply, but I cannot add any class as well, the textarea and input is generated automatically. On the client side there are buttons to input textarea, buttons, input fields..etc...
@19eggs you have given class someclass to all textarea and textbox which is common class you can use the class.
Like I said before, I have no control of adding classes and ids as they are created dynamically.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.