regular expression where underscore NOT in string.

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.
LVL 10
dij8Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raju SrivatsavayeSoftware EngineerCommented:
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..."
label.text=visible
end if
0
dij8Author Commented:
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.
0
Raju SrivatsavayeSoftware EngineerCommented:
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
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

dij8Author Commented:
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.
0
Raju SrivatsavayeSoftware EngineerCommented:
Cool,No probs
0
TheMegaLoserCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.