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>
<script src=""></script>

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


<button>Click to check special chars</button>
<input type="text" name="Firstname" value="" id="firstname">
Updated 13-Mar-19 4:12am

1 solution

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

DGKumar 13-Mar-19 9:21am
The textbox should accept these charatcers _@.!$%^#&+-{}=^~`,; and alpha numeric

I have used the below code is ti wrong

var txt = String.fromCharCode(e.which);

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