Click here to Skip to main content
13,287,834 members (51,434 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I have a text box...I need to validate the textbox value against multiple values like "4567","6754","7842"

How do i do them without using OR or elseif
Dim validCodes As String() = New String() {"5962", "5966", "5967", "7995")

I tried storing those multile values in an array and then txtSICCode.Contains(validCodes)...

This does not work.Please help.
Posted 22-Feb-13 9:39am
Updated 22-Feb-13 9:46am
BC @ CV 22-Feb-13 15:13pm
Is txtSICCode a string pulled from someTextBox.Text, if not then the problem is that you are using the Contains from the TextBox object and it is looking through child controls not the text property value. See Solution 3.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Use a Select Case statement:
Select Case myTextBox.Text
	Case "1234"
		Exit Select
	Case "2345"
		Exit Select
End Select
vidkaat 22-Feb-13 14:47pm
I dont want to go for a switch case as all the checks has the same error message.
ThePhantomUpvoter 22-Feb-13 14:55pm
Why did you not tell us in your original posting that you was getting an error message?
OriginalGriff 22-Feb-13 15:03pm
I suspect he means if it matches, it's an error. I could well be wrong though, he doesn't seem too clear.
vidkaat 22-Feb-13 15:10pm
i just wanted to know the logic of comparing the textbox value against multiple values. I tried putting the multiple values in an array and then comparing it.But it does not seem to work.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You did it backwards. You asked if the text in the textbox Contains a character.

You need to do it the other way around.
If validCodes.Contains(myTextBox.Text)

Keep in mind that this is not a "production quality" piece of code as this method is case- and whitespace-sensitive. If the user types " 2345", it will not be found in the valid code list.
vidkaat 22-Feb-13 14:56pm
i tried that...But i am not getting Contains when i place a dot after Validcodes...No clue y?
Dave Kreskowiak 22-Feb-13 15:30pm
Yeah, like I said, if the user doesn't type the code EXACTLY as it appears in your list, it's not going to match. If your code is 4 characters long and they type 5 characters, bzzzzzz, it's no longer a match.

You haven't provided enough detail about the input you're dealing with because you just said if they type "1234.". More detail about your input, like it's expected format and any options, is needed to come up with any kind of solution.
vidkaat 22-Feb-13 15:34pm

My input is an integer which is my text box value. I am already doing the validation for my textbox to be an intger and of length 4...So no worries abt that. Now i need to compare my textbox value which is an integer against multiple values like 5432,6521,7654...Hope u get my point.

Dave Kreskowiak 22-Feb-13 17:15pm
Oh, I see what happened. My mistake. I never use arrays for much anymore. I usually use List<type>. If you do this, it works:
List<string> validCodes = new List<string> { "5962", "5966", "5967", "7995" };
if (validCodes.Contains(sometext.text)) ...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Use the Contains of the Text property like so:

For Each code As String In validCodes
	If txtSICCode.Text.Contains(code) Then
	End If
vidkaat 22-Feb-13 15:20pm
i TRIED THIS .iT SHOWS AN Error"Valueof 1-dimensional array of string cannot be converted to String"...
BC @ CV 22-Feb-13 15:43pm
Check the updated solution.
vidkaat 22-Feb-13 16:20pm
Thank u so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100