473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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.
Nov 21 '05 #1
14 1502
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/de...sOperators.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


"tranky" <NO****@THANKY.YOU> wrote in message
news:d8*********************@news3.tin.it...
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.

Nov 21 '05 #2

| 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
Nov 21 '05 #3
> 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(TextB ox1.Text, rx) Then
REM Hey! There's undesirable character!
Else
REM All right!
End If
~

Best regards,

Roman
Nov 21 '05 #4

| "[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.
Nov 21 '05 #5
Dragon,

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

"Dragon" <no@spam.please> wrote in message
news:eG**************@TK2MSFTNGP10.phx.gbl...
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(TextB ox1.Text, rx) Then
REM Hey! There's undesirable character!
Else
REM All right!
End If
~

Best regards,

Roman

Nov 21 '05 #6
> 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@b.com> wrote in message news:cE****************@fe05.lga...| "[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.

Nov 21 '05 #7

| 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.
Nov 21 '05 #8

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.
"Some Guy" <no*****@nowhere.com> wrote in message
news:uE**************@TK2MSFTNGP15.phx.gbl...
|> 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@b.com> wrote in message news:cE****************@fe05.lga...
| >| "[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.
| >
| >
|
|
Nov 21 '05 #9
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


"" <a@b.com> wrote in message news:cE****************@fe05.lga...
| "[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.

Nov 21 '05 #10
< #2 that the period needs a backslash behind it

I think you mean in front of it don't you?
"Joseph Bittman MCSD" <Ry*********@msn.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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


"" <a@b.com> wrote in message news:cE****************@fe05.lga...
| "[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.


Nov 21 '05 #11
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" <no*****@nowhere.com> wrote in message
news:OE****************@TK2MSFTNGP14.phx.gbl...
< #2 that the period needs a backslash behind it

I think you mean in front of it don't you?
"Joseph Bittman MCSD" <Ry*********@msn.com> wrote in message
news:uu**************@TK2MSFTNGP12.phx.gbl...
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


"" <a@b.com> wrote in message news:cE****************@fe05.lga...
| "[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.



Nov 21 '05 #12
THANKS TO ALL!!!
Nov 21 '05 #13
> 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.

Nov 21 '05 #14
August 6, 2005

More than welcome! :-)

--
Joseph Bittman
Microsoft Certified Solution Developer

Web Site: http://71.39.42.23
Static IP


"tranky" <NO****@THANKY.YOU> wrote in message
news:iW*********************@news3.tin.it...
THANKS TO ALL!!!

Nov 21 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
3
by: Mazin07 | last post by:
I have a script that makes multiple requests to several sites. However, each request takes a little while. How can I get the PHP script to output data to let the user know that it's checking? ...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
1
by: Gidi | last post by:
hi, how can i check that the input that the user inserts is of the type i need. for expample if i want the user to insert only numbers how can i check that he hasn't insert letters or something...
8
by: Dooglo | last post by:
How do I check to see if my textbox has a alpha character in it? I don't want alpha characters, numeric only. Thanks Dooglo
9
by: D. Shane Fowlkes | last post by:
(ASP.NET 2 / VB) Question - How can I write a If statement to see if a control (textbox) actually exists on a page? Upon page_load, a certain control may or may not be visible on the page so I...
1
by: ravipatil | last post by:
hi i am adding two numbers in C#.net & i want to check wheather first textbox is empty or not. if empty, then it must display "enter the first number" otherwise value is stored in first textbox1....
0
by: teo | last post by:
Hallo, in a TextBox I implemented the AutoComplete function So the little AutoComplete window appears under the TextBox. But the Font is too big and the window Size is too little. How can I...
3
by: jc | last post by:
Hello. I have a gridview column item that i want to not make visible if the bound data in that cell is less than a value in a textbox. However, I notice at the time my code checks a function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.