Click here to Skip to main content
14,774,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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>
Posted
Updated 13-Mar-19 4:12am

1 solution

This will allow A-Z, a-z, and 0-9:

/[^\w]|_/g
   
Comments
DGKumar 13-Mar-19 9:21am
   
HI
The textbox should accept these charatcers _@.!$%^#&+-{}=^~`,; and alpha numeric

I have used the below code is ti wrong

$(function(){
$('#t').keypress(function(e){
var txt = String.fromCharCode(e.which);

if(!txt.match(/[A-Z0-9+#@.!$%&-(){}=^~,:';]/))//+#-.
{
alert("invalid");
return false;
}
});
});
#realJSOP 13-Mar-19 10:37am
   
So in other words, you want to let the user type in anything he wants? That's pretty much every printable character on the keyboard.
DGKumar 14-Mar-19 5:16am
   
Yes here only restrict * and a-z
#realJSOP 14-Mar-19 7:00am
   
I still don't know what you want. What characters do you want to allow?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900