Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi 
How to restrict the special characters being entered in textbox (ASP.Net, C#.Net, jQuery).
My Current Validation is working as expected [when user types in textbox],
 but the problem is it’s getting failed when user Copy Special Characters (#,{,},|) and Paste into the textbox then it’s not working. 
How can I prevent the user to Type/Copy Paste Special Characters into my Textbox?

Please assist me. 
Thanks.


What I have tried:

My Current Validation is working as expected [when user types in textbox],
but the problem is it’s getting failed when user Copy Special Characters (#,{,},|) and Paste into the textbox then it’s not working.
Posted
Updated 15-Aug-17 7:36am
Comments
BillWoodruff 14-Aug-17 14:58pm    
What are you doing now when a paste is done ? Anything ? In WinForms you can use a MaskedTextBox for this. Foe ASP,NET.JQuery, possibly this mifht be useful:

https://stackoverflow.com/questions/3096100/i-need-masked-textbox-in-asp-net
Kornfeld Eliyahu Peter 15-Aug-17 1:14am    
How do you handle user input? Events? 3rd party? Do you looking for 'paste' event?

1 solution

Yugesh, try this:
$("#txtBoxId").bind("paste", function(e){    
    var pastedText = e.originalEvent.clipboardData.getData('text');
    validate(pastedText);
} );


Remember this time in validate function you have to validate whole pasted data for any special character presence.

Hope it helps.
 
Share this answer
 

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