We help IT Professionals succeed at work.

regular expression where underscore NOT in string.

dij8 asked
Medium Priority
Last Modified: 2008-03-17
I have a text box I want to make sure an underscore (_) character is NOT included anywhere.  I have added a regular expression validator but I can't work out what the regular expression should be.  I thought it would be simple but apparently it isn't.

Basically I have tried

None of these work.  All I want is to make sure there is no underscore in the text.  Please can someone give me the regular expression for testing that.

Thank you.
Watch Question

Raju SrivatsavayeSoftware Engineer

I donot quite know the expression but I can show you way to do that:

do this:

  Dim strText As String
        strText = TextBox1.Text
        Dim result As integer = strText.IndexOf("_")//THis checks for the underscore..Now result will be -1 if "_" isnt found.

So you can validate this in the clientside in buttonclick event itself like this:

if result>=0 then

Dim strInfo As String = "You cannot enter Underscore in this field"
                Dim strScript As String = "<script language=JavaScript>"
                strScript += "alert(""" & strInfo & """);"
                strScript += "</script>"
                If (Not Page.ClientScript.IsStartupScriptRegistered("clientScript")) Then
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", strScript)
                End If
    end if    

THis will work just fine...You can also display this in a label if do not want a javascript alert.
if i result>=0 then
label.text="You cannot enter..."
end if


I know I can go with a JavaScript option (clientside validation is imperative) but it seems like an awful lot of work for something that should be as simple as using a built in tool.

The extra work involved is the number of places I need to consider how to make sure the script is run on form submit (I can't put it in the forms "onsubmit" as I am dealing with a plugged in user control not the outside page area that includes the form tag) and on top of that I need to consider which text field is failing and therefore which error message to display, again using JavaScript and teh lieks of display:block; and display:none;  ASP.Net has validation controls (not good ones but they are there all the same) and surely regular expressions are smart enough to return false when a string matches instead of true if that is what is desired.  A simple WHERE NOT clause.
Raju SrivatsavayeSoftware Engineer

The javascript I gave you should be placed under the button control...ANyway since you have many validations to perform
it would be better to place a label beside the textbox just like you would place a validator and perform the code


What you have suggested happens after postback.  Sure, it won't save whatever if I do it all in the right place but I want a simple way of doing this client side without even attempting a postback.  This kind of thing is HTML 101.

ASP.Net has a regular expresion validator.  I want to use this tool.  I want a regular expression that makes sure a certain character (in this case an underscore) is NOT included in the text.

Thank you srivatsavaye for your input.  However, I don't want a scripted option (where I develop the script, I know regular expression validators are still script), I want what I thought would be a very simple, yet elluding me, regular expression.
Raju SrivatsavayeSoftware Engineer

Cool,No probs
The right expression is:


[] lets you define a set of charachters
^ first in the set sais you dont want any of them
* allows multiple charachters

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.