Click here to Skip to main content
14,920,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to apply Regular Expression Validation to a textbox User Control and it's only working when I enter something at the end of the text in the textbox. And when I type something somewhere in the middle of the text, it's not working.

For Example: Hey Man! (When I type '!' at the end of the text, my Code's working fine) Hey! Man! (But when I insert '!' somewhere in the middle of the text after the entire text is typed, not working)

Below is my Code.

Any help would be greatly appreciated! Thanks!

What I have tried:

$("textarea[id$=<%= TxtValue.ClientID %>]").keyup(function () {
            var txt = $(this).val();
            var regex = new RegExp('[^0-9a-zA-Z-,_.\:\s]+$');
            var vldttxt = regex.test(txt);
            if (txt.length > 0 && vldttxt === true) {
            var noSpclChar = txt.replace(regex, "");
            this.value = noSpclChar;
Updated 27-Dec-18 6:06am

In theory, "Hey man!" and "Hey! Man!" should both be recognised, because they both end with an exclamation mark. Indeed when I check with Expresso[^] or with an online JS regex tester they are both matched without problems.
"Hey! Man" on the other hand, would not, because your regex string specifically requires a special character as the final character in the string.

If you are trying to enforce password rules "must include a special character", then remove the '$' at the end of your regex, and it will match one or move special characters at any point in the input.
Member 13952925 27-Dec-18 12:02pm
You're the man! It works now. Now, I'm facing a tiny issue ie. when I type something in the middle, my Code works fine, but the Cursor moves away to the end of the text. This is because I'm doing a text reset by using it's .value property. Any idea so that the Cursor stays right there when I type a Special Character in the middle. Thanks!
OriginalGriff 27-Dec-18 12:20pm
That's probably because you are setting the Text property of the TextBox inside the TextChanged event handler - which automatically moved the caret (technical name for the vertical bar input point) to the end of the text.
If you want the cursor to not move, save the SelectionStart property before you set the Text, and restore it afterwards. Don't blame me, I didn't write it! :laugh:
Member 13952925 27-Dec-18 14:57pm
That's a great idea! But how do I do that? I mean I'm not at all familiar with the SelectionStart property. Can you please help me?
OriginalGriff 28-Dec-18 1:44am
So look in the documentation!
It's not complicated, just google "C# TextBox.SelectionStart" and follow the link to MS Docs.
The dollar sign means find at the end of the source data. See JavaScript RegExp Reference[^].
Member 13952925 27-Dec-18 12:13pm
Thanks for the instant response. It's working. The issue now I'm facing is that since I'm doing a text reset using it's .value property, when I type a Special Character in the middle, the Cursor's moving away to the end of the text. Is there any other way where the Cursor stays right there when I type a Special Character in the middle?
Richard MacCutchan 27-Dec-18 12:17pm
Sorry, I don't know jQuery.
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Member 13952925 27-Dec-18 12:10pm
Thanks for taking your time in posting these links. They will definitely be helpful for me in the future. Will go through them for sure. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900