Little problem for me...about checking a textbox!

  • Thread starter Thread starter tranky
  • Start date Start date
T

tranky

Hi, i'm italian...so...excuse me for my english.
I've a little problem....in what manner i can check a textbox for know if it
contain only character from A-Z (a-z), numbers (0-9), and underscore (-) and
dot (.) ?!?!?!?

I use vb.net.

thank to all.
 
August 5, 2005

Regular Expressions allow you to easily check whether text contains
certain valid (or invalid) characters. It is located in the
System.Text.RegularExpressions namespace:

Imports System.Text.RegularExpressions

dim valid as boolean = false

dim expression as string = "[A-Za-z/d-.]{0,}"
'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
allowing unlimited numbers of those chars. Then use this to check whether it
is true or not:

valid = regex.IsMatch(InputString,Expression) ' Checks whether the
inputstring matches the rules in the expression. (the signature might have
to be slightly modified because of my poor memory) Regex is a shared class.

if valid = false then
msgbox("Invalid input!!!")
end if

the /d in the regular expression should stand for 0-9; somebody correct me
if I did this wrong!

You might look at:

http://msdn.microsoft.com/library/d...elp/html/fogrfRegularExpressionsOperators.asp

for more info! Hope this helps and have a great day!

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP
 
| the /d in the regular expression should stand for 0-9; somebody correct me
| if I did this wrong!

\d and \. were incorrect (just letters, numbers, and underscores...no
period).

it may be worth mentioning in what event to place this code. you can either
validate as the user types or after all the input has been provided and the
textbox begins validation.

to simplify the all of the above:

private sub yourTextBoxEvent( _
byval sender as object, _
[args here for appropriate event]_
) _
handles yourTextBox.[appropriate event])
if not typeof sender is textbox then return
dim textBox as textbox = sender
dim exp As new regex("^[a-z\d_]*$", regexoptions.ignorecase)
if exp.ismatch(textBox.text) then return
msgbox("invalid input supplied")
end sub

hth,

me
 
the /d in the regular expression should stand for 0-9; somebody correct me
if I did this wrong!

Correcting. 8=]

0-9 is \d

"[A-Za-z/d-.]{0,}" — it doesn't work!
'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
allowing unlimited numbers of those chars.

But, it would check if input ^contains^ valid string, but not if it's valid
wholly.

I offer the following:

~
Dim rx As String = "[^\.\d\w_]"
If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, rx) Then
REM Hey! There's undesirable character!
Else
REM All right!
End If
~

Best regards,

Roman
 
| "[A-Za-z/d-.]{0,}" — it doesn't work!

no, it doesn't.

"[^\.\d\w_]"

nor does that.

this *does* based on the op's requirements:

"^[a-z\d_]*$"

the requirements were:

a-z
A-Z
0-9
_

why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
string has to *start* with a *single correct character* but can be followed
by *any* character.
 
Dragon,

This "[^\.\d\w_]" will allow an empty string. Should be "[^\.\d\w_]*"

Dragon said:
the /d in the regular expression should stand for 0-9; somebody correct
me
if I did this wrong!

Correcting. 8=]

0-9 is \d

"[A-Za-z/d-.]{0,}" - it doesn't work!
'This exp ^^ should check for A-Za-z0-9 and the - & . characters with it
allowing unlimited numbers of those chars.

But, it would check if input ^contains^ valid string, but not if it's
valid
wholly.

I offer the following:

~
Dim rx As String = "[^\.\d\w_]"
If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, rx) Then
REM Hey! There's undesirable character!
Else
REM All right!
End If
~

Best regards,

Roman
 
why are periods in both you guy's exps?

Because he wanted to check for a period

also, "[^\.\d\w_]" only says the
string has to *start* with a *single correct character* but can be
followed
by *any* character.

Did you try it?

 said:
| "[A-Za-z/d-.]{0,}" - it doesn't work!

no, it doesn't.

"[^\.\d\w_]"

nor does that.

this *does* based on the op's requirements:

"^[a-z\d_]*$"

the requirements were:

a-z
A-Z
0-9
_

why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
string has to *start* with a *single correct character* but can be
followed
by *any* character.
 
| Because he wanted to check for a period

"he" not being the op. i didn't see that the op wanted a period. did i miss
it?

| > also, "[^\.\d\w_]" only says the
| > string has to *start* with a *single correct character* but can be
| > followed
| > by *any* character.
|
| Did you try it?

actually, i did not...and, i mis-read the exp and thought the ^ was outside
the character class.
 
you're right! the op *did* want to check for a period...had to go back and
look again. ;^)

i don't think i saw whether he said a blank string was also valid or if it
had to have some kind of valid input.


|> why are periods in both you guy's exps?
|
| Because he wanted to check for a period
|
|
| > also, "[^\.\d\w_]" only says the
| > string has to *start* with a *single correct character* but can be
| > followed
| > by *any* character.
|
| Did you try it?
|
| >| "[A-Za-z/d-.]{0,}" - it doesn't work!
| >
| > no, it doesn't.
| >
| > "[^\.\d\w_]"
| >
| > nor does that.
| >
| > this *does* based on the op's requirements:
| >
| > "^[a-z\d_]*$"
| >
| > the requirements were:
| >
| > a-z
| > A-Z
| > 0-9
| > _
| >
| > why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
| > string has to *start* with a *single correct character* but can be
| > followed
| > by *any* character.
| >
| >
|
|
 
August 5, 2005

LOL Thanks for:

#1 the reminder that it is backslashes and not forwardslashes.
#2 that the period needs a backslash behind it

Now, I think there is something that we all missed.... (with the assumption
that blank strings are okay), is to append the ^ to the front AND and $ to
the back... which means that the WHOLE string must conform to the pattern
and not just one part (which can be just 1 char). This is a common security
slip up that even I made a mistake in my original post. Also, I always like
to list out things like A-Za-z so that I never accidentally forget that a
char set contains something I didn't want like a char return, etc. So:

if regex.ismatch(inputstring,"^[A-Za-z\d\._]{0,}$") = false then
msgbox("Invalid")
else
msgbox("Valid")
end if

This will work and I have tested it. Hope this helps and people are free to
comment!

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP
 
< #2 that the period needs a backslash behind it

I think you mean in front of it don't you?


Joseph Bittman MCSD said:
August 5, 2005

LOL Thanks for:

#1 the reminder that it is backslashes and not forwardslashes.
#2 that the period needs a backslash behind it

Now, I think there is something that we all missed.... (with the
assumption that blank strings are okay), is to append the ^ to the front
AND and $ to the back... which means that the WHOLE string must conform to
the pattern and not just one part (which can be just 1 char). This is a
common security slip up that even I made a mistake in my original post.
Also, I always like to list out things like A-Za-z so that I never
accidentally forget that a char set contains something I didn't want like
a char return, etc. So:

if regex.ismatch(inputstring,"^[A-Za-z\d\._]{0,}$") = false then
msgbox("Invalid")
else
msgbox("Valid")
end if

This will work and I have tested it. Hope this helps and people are free
to comment!

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP




 said:
| "[A-Za-z/d-.]{0,}" - it doesn't work!

no, it doesn't.

"[^\.\d\w_]"

nor does that.

this *does* based on the op's requirements:

"^[a-z\d_]*$"

the requirements were:

a-z
A-Z
0-9
_

why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
string has to *start* with a *single correct character* but can be
followed
by *any* character.
 
August 5, 2005

Depends on how you normally say "behind or in-front"... LOL I think you got
the point! :-)

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP




Some Guy said:
< #2 that the period needs a backslash behind it

I think you mean in front of it don't you?


Joseph Bittman MCSD said:
August 5, 2005

LOL Thanks for:

#1 the reminder that it is backslashes and not forwardslashes.
#2 that the period needs a backslash behind it

Now, I think there is something that we all missed.... (with the
assumption that blank strings are okay), is to append the ^ to the front
AND and $ to the back... which means that the WHOLE string must conform
to the pattern and not just one part (which can be just 1 char). This is
a common security slip up that even I made a mistake in my original post.
Also, I always like to list out things like A-Za-z so that I never
accidentally forget that a char set contains something I didn't want like
a char return, etc. So:

if regex.ismatch(inputstring,"^[A-Za-z\d\._]{0,}$") = false then
msgbox("Invalid")
else
msgbox("Valid")
end if

This will work and I have tested it. Hope this helps and people are free
to comment!

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP




 said:
| "[A-Za-z/d-.]{0,}" - it doesn't work!

no, it doesn't.

"[^\.\d\w_]"

nor does that.

this *does* based on the op's requirements:

"^[a-z\d_]*$"

the requirements were:

a-z
A-Z
0-9
_

why are periods in both you guy's exps? also, "[^\.\d\w_]" only says the
string has to *start* with a *single correct character* but can be
followed
by *any* character.
 
This "[^\.\d\w_]" will allow an empty string. Should be "[^\.\d\w_]*"

And this won't allow a valid string. Actually it matches any string.

Though, I missed some detail...
contain only character from A-Z (a-z)

\w matches any letter, so regex would be: "[^A-Za-z\d\._]".

I didn't see if tranky worried about blank strings, however if he did,
"[^A-Za-z\d\._]|^$" will do the job.
 
Back
Top