regular expression where underscore NOT in string.

Posted on 2006-03-23
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.
Question by:dij8
    LVL 9

    Expert Comment

    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
    LVL 10

    Author Comment

    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.
    LVL 9

    Expert Comment

    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
    LVL 10

    Author Comment

    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.
    LVL 9

    Expert Comment

    Cool,No probs
    LVL 12

    Accepted Solution

    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

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now