如何使用jquery限制文本框中的特殊字符? [英] How to restrict special characters in textbox using jquery?

查看:117
本文介绍了如何使用jquery限制文本框中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提交了姓名,而且应该只接受一些角色。



我尝试过:



我尝试过以下代码



I have filed with Name and that should be accept some characters only.

What I have tried:

I Have tried with the below code

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

  $("button").click(function(){
  
  var txtvalue=$("#firstname").val();
 
  if(isValid(txtvalue)){
            alert("Name can have only specific chras only.");
        }
        else{ alert("Accepted.");}
  });
    function isValid(str){
      return /[_@.!$%^#&+-{}=^~`,;]/g.test(str);
     
}


});
</script>
</head>
<body>


<button>Click to check special chars</button>
<input type="text" name="Firstname" value="" id="firstname">
</body>
</html>

推荐答案

(文件).ready(function(){
(document).ready(function(){


(button)。click(function(){

var txtvalue =
("button").click(function(){ var txtvalue=


(#firstname)。val();

if(isValid(txtvalue)){
alert(Name只能有特定的chras。);
}
else {alert(Accepted。 );}
});
函数isValid(str){
return /[_@.!
("#firstname").val(); if(isValid(txtvalue)){ alert("Name can have only specific chras only."); } else{ alert("Accepted.");} }); function isValid(str){ return /[_@.!


这篇关于如何使用jquery限制文本框中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆