Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 631
  • Last Modified:

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.
0
dij8
Asked:
dij8
  • 3
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now