How to allow forward slash in javascript Regex?

Here, I have blocked to type special characters for all input type in my mobile browser.
But it will allow all alphabets and numbers.

My requirement is to allow the users to type only / (forward slash) for particular field (for example: Date field).

Is it possible in a simple way?

Instead of assigning class name for all input types except for the particular field which I need / (forward slash)?

   $(document).on('keypress','input', function(event){  
    var regex = new RegExp("^[a-zA-Z0-9]+$");
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
       return false;

Note: I need to allow forward slash to particular class instead of doing all input fields.

Simple: I need to block special characters for all input fields expect the particular class (let’s say )

I’m not asking for the solution with the same regex function. I need separate click event to allow only slash for the particular class ‘someclassname’

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.