简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM