How to prevent ’ character being entered into a textbox? - c#

I am trying to prevent the ’ character from being entered into a textbox.
The problem is if you copy and paste the ’ character it becomes a right single quotation, which causes an error in the sql when trying to save the textbox to the database.
You can see this here in the
unicode lookup
If you manually enter in ' then it is an apostrophe and this character is accepted.
The problem is people are copying and pasting this character from emails into the textbox and it gets converted to the single right quote.
Regex regex = new Regex(#"^[a-zA-Z0-9]*$");
Match match = regex.Match(txtName.Text);
if (!match.Success)
{
DisplayMsg("Name contains invalid character");
}
Is there a way to still allow the user to enter an apostrophe but prevent the single right quotation?

Rather than preventing users from entering (especially if they are copying and pasting), you'd be better off replacing the character yourself if you really need to get rid of it:
txtName.Text = txtName.Text
.Replace((char) 0x2018, '\'') // ‘ ‘
.Replace((char) 0x2019, '\'') // ’ ’
.Replace((char) 0x201C, '"') // “ “
.Replace((char) 0x201D, '"'); // ” ”
This way, you won't get in the way of your users and you'll still remove this character.
However, it does sound like you might be building up queries using string concatenation, and this is a more serious problem!

Is it possible to just encode the strings using HttpUtility.HtmlEncode("A string with lot's of q'o'u't'e's that should work f'i'n'e'") which results in A string with lot's of q'o'u't'e's that should work f'i'n'e'...
Since I can't comment on anyone else's answers other than my own, I have to do it like this.
Building off of the solution from #Richard, it could be made a little more readable for others that will have to follow after you.
string someString = "This has a left quote: ’\nThis has a right quote: ‘";
string sanitized = someString.Replace(HttpUtility.HtmlDecode("‘"), "'")
.Replace(HttpUtility.HtmlDecode("’"), "'");
Console.WriteLine(sanitized);
Edit: Misunderstood the original intent.

Related

C# Regex.Replace not matching and removing double quotes

I googled for an answer and I found some questions here on Stack Exchange asking similar question but they didn't help me. For example, I found C# regex - not matching my string but the answers given are way too complicated for me to understand. I don't know or understand regex. All I want to do is strip a double quote from a string.
To put my question simply, I have a string "\"123.456\"" and I need to remove the "\""
so I made my expression "[^\w\\"]" and after calling
string myString Regex.Replace("\"123.456\"", "[^\\w\\\"]", "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));
myString is "\"123.456\"". I just need to know what my expression should be. I won't be able to understand any lengthy discussions or lectures on learning regex.
I got my example directly from Microsoft at http://msdn.microsoft.com/en-us/library/844skk0h(v=vs.110).aspx so basically all I did was replace the ".#-" with "\"".
UPDATE
Apparently trying to ask a simple question only attracts trolls. I didn't want to get too complicated because I didn't want all you hard working busy people to spend too much time answering the wrong question. I was trying to be nice.
We have a situation where we need to parse input files from several clients and going forwards, the number of clients will increase and there also the number of files from each client will increase.
We found that in several of our clients' transmitted files many fields will have various extra characters. We don't know how or why those characters are in there and our clients aren't telling. (if you want to know why they aren't telling, please move along, these aren't the questions you are looking for)
So, we have many files from many clients each with many rows with many fields of data and we need to strip out "bad" characters.
I took Microsofts method and changed it a bit to be more dynamic.
private string CleanInput(string strIn, string chars)
{
// Replace invalid characters with empty strings.
try
{
string regexString = string.Format(#"[^\w\{0}]", chars);
return Regex.Replace(strIn, regexString, "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (RegexMatchTimeoutException)
{
return string.Empty;
}
goal here is to be able to strip out any characters that don't belong dynamically But we can't just hard code those characters because not all fields will have any of these characters, and more importantly, some fields will have some bad characters along with other characters which are not to be considered bad for that field but may be considered bad for other fields.
With me so far?
So, in trying to get my work done by Friday (yes, tomorrow), I decided to start slowly with only a couple of known bad characters from 3 input files. So far, those characters are single quote, dash, double quote, dollar sign, comma. But not all the fields in my 3 files need these characters stripped, so I intend to call the CleanInput method only on those fields that need it, and only for the characters that we need stripped.
OK, so while I was testing, I discovered on one field, where we want to strip the comma, single quote, double quote and dollar sign, it was not removing the double quotes (an apparently the backslashes too). So I debugged this issue by first passing in only the comma -that worked. Then I tried passing in only the single quote - that worked. Then I passed in the dollar sign - that worked. Then I passed in the escaped double quote -and that didn't work - the double quotes are still in the string. So I simplified my test in a new console project and I hard coded the string and I called my method just to make sure nothing else could be interfering with it.
I hope and pray no one spends hours of their precious time trying to reconfigure my input files or attempting to teach me the end all be all of regex programming. I have to get this done by tomorrow. Please, I only want to know how to strip the double quote (and apparently the backslashes too) from the given string.
Rather than getting regex involved, perhaps you can just use Replace?
var myString = "\\\"123.456\\\"";
var myCleanString = myString.Replace(#"\""", "");
You are matching on a negated group (the [^] bit). This matches any character not in the square brackets and replaces it. You want to replace anything that is in the group which you can do by just placing the characters you wish to replace inside the square brackets and remove the negation (^):
private static string CleanInput(string strIn, string chars)
{
// Replace invalid characters with empty strings.
try
{
string regexString = string.Format(#"[{0}]", chars);
return Regex.Replace(strIn, regexString, "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (RegexMatchTimeoutException)
{
return string.Empty;
}
}
You would use the negative version if you knew what you wanted to include rather than exclude. For example if you knew you only wanted numbers and the period character you could do:
string myString = Regex.Replace("\"123.456\"", "[^\\d.]", "",
RegexOptions.None, TimeSpan.FromSeconds(1.5));

Match up everything before STRING or STRING

I've searched for hours and already tried tons of different patterns - there's a simple thing I wan't to achive with regex, but somehow it just won't do as I want:
Possible Strings
String1
This is some text \0"§%lfsdrlsrblabla\0\0\0}dfglpdfgl
String2
This is some text
String3
This is some text \0
Desired Match/Result
This is some text
I simply want to match everything - until and except the \0 - resulting in only 1 Match. (everything before the \0)
Important for my case is, that it will match everytime, even when the \0 is not given.
Thanks for your help!
You can try with this pattern:
#"^(?:[^\\]+|\\(?!0))+"
In other words: all characters except backslashes or backslashes not followed by 0
I like
#"^((?!\\0).)*"
Because it's very easy to implement with any arbitrary string. The basic trick is the negative lookahead, which asserts that the string starting at this point doesn't match the
regular expression inside. We follow this with a wildcard to mean "Literally any character not at the start of my string. If your string should change, this is an easy update - just
#"^((?!--STRING--).)*)"
As long as you properly escape that string. Heck, with this pattern, you're merely a regex_escape function from generating any delimiter string.
Bonus: using * instead of + will return a blank string as a valid match when your string starts with your delimiter.

Whitespace Regular expression asp.net

I have a textbox field, and i want to regulate whats typed in.
I dont want users to type more or less then 6-10 characters. here is my regex for limiting the characters ^.{6,10}$ I got this part working, but I also dont want users to type in whitespace(space). Now i am able to detect whitespace from the beginning of the input and ending of the input, but not if the user types in space in middle of the text. see example.
" testing" = regulator detects the space in the beginning. regex i use ^[^\s].+[^\s]$
"testing " = regulator detects the space in the end. regex i use here is same as abow
"test ing" = regulator does not detect the space in the middle. tried different regex with no luck.
how can i create a regulator which will do all that i require?
It is the problem with your . which matches everything
Do this
^[^\s]{6,10}$
[^\s] matches any character except space
OR
^\w{6,10}$
\w is similar to [\da-zA-Z_]
Some1.Kill.The.DJ answers is great, but for your personnal knowledge, you can also use the following:
^\S{6,10}$
\S matches any characters except space the same way [^\s] does

How to decode special characters (blank square unreadable) in c#?

I have a problem that i can manage to resolve, i need to replace unreadable characters (i can't paste it here since it's not taken into acount but it shows like a blank square in the visual C# debugger.
When those are inserted in the sql database they are replaced by a ? but i don't want it... I tried to do a simple replace on the string but visual c# makes the pasting of such characters impossible.
You can't. When you see the red squares, you've already lost the relevant character info and you can't convert them back.
You can replace any character if you only know the unicode character code:
s = s.Replace('\u0080', ' ');
You can use a regular expression to replace any character outside of a set of allowed characters:
s = Regex.Replace(s, #"[^0-9A-Za-z]", " ");
Another alternative is to use a unicode data type in the database, so that it can handle any character that you have in your string.

how to merge or inject "#" character in a string including escape characters without definning the string varibale from scratch in C#

hi , I have 2 related questions.
1)suppose we have:
string strMessage="\nHellow\n\nWorld";
console.writeln(strMessage);
Result is:
Hellow
World
Now if we want to show the string in the original format in One Line
we must redefine the first variable from scratch.
string strOrignelMessage=#"\nHellow\n\nWorld" ;
console.writln(strOrignelMessage);
Result is:
\nHellow\n\nWorld --------------------->and everything is ok.
i am wondering is there a way to avoid definning
the new variable(strOrignelMessage) in code for this purpose and just using only
the first string variable(strMessage) and apply some tricks and print it in one line.
at first i tried the following workaround but it makes some bugs.suppose we have:
string strMessage="a\aa\nbb\nc\rccc";
string strOrigenalMessage=strMessage.replace("\n","\\n").replace("\r","\\r");
Console.writeln(strOrigenalMessage)
result is :aa\nbb\nc\rccc
notice that befor the first "\" not printed.and now my second question is:
2)How we can fix the new problem with single "\"in the string
i hope to entitle this issue correctly and my explanations would be enough,thanks
No, because the compiler has already converted all of your escaped characters in the original string to the characters they represent. After the fact, it is too late to convert them to non-special characters. You can do a search and replace, converting '\n' to literally #"\n", but that is whacky and you're better off defining the string correctly in the first place. If you wanted to escape the backslashes in the first place, why not put an extra backslash character in front of each of them:
Instead of "\n" use "\\n".
Updated in response to your comment:
If the string is coming from user input, you don't need to escape the backslash, because it will be stored as a backslash in the input string. The escape character only works as an escape character in string literals in code (and not preceded by #, which makes them verbatim string literals).
if you want "\n\n\a\a\r\blah" to print as \n\n\a\a\r\blah without # just replace all \ with \\
\ is the escaper in a non-verbatim string. So you simply need to escape the escaper, as it were.
If you want to use both strings, but want to have only one in the code then write the string with #, and construct the other one with Replace(#"\n","\n").
explanations for Anthony Pegram (if i understand u right) and anyone that found it usefull
i think i find my way in question2.
at first ,unfortunately,i thought that the
escape characters limts to \n,\t,\r,\v and
this made me confuesed becouse in my sample string i used \a and \b
and the compiler behaviuor was not understandable for me.
but finally i found that \a and \b is in
escape-characters set too.and if u use "\" without escap characters
a compile time error would be raised (its so funny when i think to My mistake again)
pls refers to this usefull msdn article for more info.
2.4.4.5 String literals
and you couldnt replace \ (single\) with \\
becouse fundamentally you couldnt have a (single \) without using
escape-characters after it in a string .so we coudnt write such a string in the code:
string strTest="abc\pwww"; ------> compile time error
and for retriving an inactived escape characters version of a string
we can use simply string.replace method as i used befor.
excuse me for long strory ,thank u all for cooperation.

Categories

Resources