Click here to Skip to main content
15,907,000 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a textbox have to filter only ' and ", except ' and " , it will accept all character, special character, alphanumeric

What I have tried:

string FreeTextRegEx { get { return @"^[a-zA-Z-0-9~+:;,/#&_@*%$!()\[\] ]*$"; } }
Posted
Updated 30-Apr-18 3:22am

Um. If I read you right, you want to accept everything except quote and double quote?
If so, that's simple:
^[^'"]*$
Will do it
 
Share this answer
 
Comments
kumarravishankar 30-Apr-18 8:41am    
public override string FreeTextRegEx { get { return @"^[a-zA-Z-0-9~+:;,/#&_@*%$!()\[\] ]*$"; } }

but when using
public override string FreeTextRegEx { get { return @"^[^'"]*$"; } }
getting error
OriginalGriff 30-Apr-18 8:47am    
And why do you think that is?
Hint: what does a single double quote do in a string?
Bryian Tan 1-May-18 16:45pm    
You have to use two double quote to escape the double quote
@"^[^'""]*$";

public override string FreeTextRegEx { get { return @"^[^'""]*$"; } }
kumarravishankar 30-Apr-18 8:57am    
as per requirement have not allowed in the textbox,
... new solution ...

To both prevent the user entering those characters at run-time, and catch the 'Paste event and remove those characters, you can use a UserControl:
using System.Text;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class NoQuoteTextBox : TextBox
    {
        private const int WM_PASTE = 0x0302;

        private StringBuilder sb;

        public NoQuoteTextBox()
        {
            InitializeComponent();
        }

        protected override void WndProc(ref Message msg)
        {
            if (msg.Msg != WM_PASTE)
            {
                // good to go here
                base.WndProc(ref msg);
            }
            else
            {
                // handle paste
                sb = new StringBuilder(Clipboard.GetText());

                if (sb.Length > 0)
                {
                    sb.Replace("\'", string.Empty);
                    sb.Replace("\"", string.Empty);

                    if (sb.Length > 0)
                    {
                        Clipboard.SetText(sb.ToString());
                        base.WndProc(ref msg);
                    }
                }
            }
        }

        // handle user keyboard entry at run-time
        private void NoQuoteTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = e.KeyChar == '\'' || e.KeyChar == '\"';
        }
    }
}


... previous solution ...

Why not prevent those characters from being entered ?
private void SomeTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\'' || e.KeyChar == '\"')
    {
        e.Handled = true;
    }
}
 
Share this answer
 
v2
Comments
OriginalGriff 30-Apr-18 10:00am    
Paste?
BillWoodruff 30-Apr-18 10:55am    
If you are asking if I pasted in that snippet, the answer is 'yes.' I also wrote it, and tested it: is something wrong with it ? thanks, Bill
OriginalGriff 30-Apr-18 10:57am    
No, Bill - what I meant was that blocking the quote and double quote keys doesn't prevent the user pasting the characters in.

I must remember not to use cogitational shortcuts ... :laugh:
BillWoodruff 30-Apr-18 11:02am    
Ahh, okay :) I take your point; the example sure doesn't handle paste.
BillWoodruff 1-May-18 16:02pm    
@OriginalGriff

If the now revised solution isn't up to your spec, we're getting a divorce :)

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