Click here to Skip to main content
15,896,541 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I have created Windows form with multiple Text boxes.

one of those text box does not accept the following characters like '=', '\' .

If user enters these characters, need to display a balloon pop-up message as 'The following characters are not valid: \ = ".

I have done this in 'Key Pressed' event to prevent these characters and displayed message.

But, while doing Copy Paste these invalid characters in text box, first it displays all the characters and then it removes invalid characters. (Since in 'Text changed' event, i have read the one by one and then removing invalid characters). and displayed a balloon message.

How to don't show the invalid characters from string even if i copy paste in text box.
(Ex: 'xyz=fg\') it should display only 'xyzfg' and balloon pop-up message.

Any help on this?

Thanks in advance.
Posted
Updated 17-Dec-14 23:12pm
v2
Comments
BillWoodruff 11-Dec-14 1:16am    
Yes, it's possible to filter a paste into a TextBox. And using 'TextChanged will potentially be very slow.

Before I post a solution, with code, let me ask you if you need to filter all of your TextBoxes in the same way, or are there different types requiring different types of filtering ?
sdileep1 11-Dec-14 23:54pm    
Filter is different in all the text boxes.

1 solution

Try this

C#
$('#txtname').bind('keypress', function (event) {
          var regex = /[!`~:;"'\^*%${}[\]#&())_?\\<>+/|]/gi;
          var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
          if (regex.test(key)) {
              event.preventDefault();
              return false;
          }
      }).bind('paste', function (event) {

          var el = $(this);
          setTimeout(function () {
              var regex = /[!`~:;"'\^*%${}[\]#&())_?\\<>+/|]/gi;
              var text = $(el).val();
              if (regex.test(text)) {
                  $(el).val('');
                  return false;
              }
          }, 100);
      });
 
Share this answer
 
v2

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