Link to home
Start Free TrialLog in
Avatar of dij8
dij8Flag for New Zealand

asked on

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.
Avatar of Raju Srivatsavaye
Raju Srivatsavaye
Flag of United States of America image

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
Avatar of dij8

ASKER

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.
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
Avatar of dij8

ASKER

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.
Cool,No probs
ASKER CERTIFIED SOLUTION
Avatar of TheMegaLoser
TheMegaLoser
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial