Link to home
Start Free TrialLog in
Avatar of us1975mc
us1975mcFlag for United States of America

asked on

Regular Expression Validator Validation Control Expression

I need a RegularExpressionValidator to validate a textbox that has only one character.  It can have only the letters A-Z, or the numbers 0-9, or a dash, or an apostrophe, or a space.  Any ideas what the ValidationExpression is?
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Try this

([A-Z]{1,1})|([0-9]{1,1})|(-)|(')|(\S)
Try this;

 
SubjectString = "121234567890-=\][poiuytrewqq';l/., " 
Try
	If Regex.IsMatch(SubjectString, "[\w\x20\x27\x2D]") Then
		' Successful match
	Else
		' Match attempt failed
	End If
Catch ex As ArgumentException
	'Syntax error in the regular expression
End Try

Open in new window

Avatar of us1975mc

ASKER

SriVaddadi,

I am still able to enter other characters.  Below is my code.


<asp:RegularExpressionValidator ID="Regex1" 
                runat="server" 
                ErrorMessage="RegularExpressionValidator" 
                ControlToValidate="Let1"
                ValidationExpression="([A-Z]{1,1})|([0-9]{1,1})|(-)|(')|(\S)" > 
            </asp:RegularExpressionValidator>

Open in new window

mgfranz,

I am really lost on this.  Do I still need a RegularExpressionValidator?  Where do I place the code?  In the Let1_TextChanged?
Protected Sub Let1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Let1.TextChanged
        Dim Subjectstring As String
        SubjectString = "121234567890-=\][poiuytrewqq';l/., "
        Try
            If Regex.IsMatch(SubjectString, "[\w\x20\x27\x2D]") Then
                Let2.Focus()
            Else
                Let1.Focus()
                MsgBox("Try Again")
            End If
        Catch ex As ArgumentException
            'Syntax error in the regular expression
        End Try
    End Sub

Open in new window

If you would rather not use ASCII parameters, you can use this;

 
SubjectString = "`~1!2@3#4$5%6^7&8*9(0)-_=+qwertyuiop[{]}\|asdfghjkl;:'zxcvbnm,<.>/? "
Try
	Dim RegexObj As New Regex("[A-Za-z0-9'\- ]")
	If RegexObj.IsMatch(SubjectString) Then
		' Successful match
	Else
		' Match attempt failed
	End If
Catch ex As ArgumentException
	'Syntax error in the regular expression
End Try

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mark Franz
Mark Franz
Flag of United States of America 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
This is a solution for what I have ask but I guess I ask the wrong question.  I will post another question tomorrow dealing with a different twist.

Thank you for your time and effort.