mohd ilyas
mohd ilyas

Reputation: 71

How to allow certain characters in input field

How can i allow only "A-Za-z0-9 &#$._-" along with "/" . I tried with jquery

$('.allow').bind('keyup blur',function(){ 

   $(this).val( $(this).val().replace(/[^A-Za-z0-9 &#$._-]/g,' ') ); }

);

Along with this i also need to allow forward slash "/".

Can anyone please help me out. It will be a great help.

Upvotes: 2

Views: 113

Answers (2)

Bibek Sharma
Bibek Sharma

Reputation: 3320

$('.allow').bind('keyup blur',function(){ 

   $(this).val( $(this).val().replace(/[^A-Za-z0-9\\ &#$._-]/g,' ') ); }

);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="allow"/>

Upvotes: 0

Tushar
Tushar

Reputation: 87203

Use ^ and $ outside of []. This will match only the allowed characters.

You need to escape the / and - in regex

/^[a-z0-9 &#$._\-\/]$/i

Upvotes: 2

Related Questions