如何允许某些字符在输入字段

How to allow certain characters in input field

本文关键字:输入 字段 字符 何允许      更新时间:2023-09-26

我怎么能只允许"A-Za-z0-9 &#$;_-"answers"/"一起。我尝试使用jquery

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

除此之外,我还需要允许正斜杠"/"。

有谁能帮帮我吗?这将是一个很大的帮助。

[]之外使用^$。这将只匹配允许的字符。

需要转义regex

中的/-
/^[a-z0-9 &#$._'-'/]$/i

$('.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"/>