19eggs
19eggs

Reputation: 195

Check for special characters

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.');
}}

Upvotes: 0

Views: 922

Answers (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.');
    }
});

Upvotes: 1

Related Questions