Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

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.
Thanks.
Posted
Updated 22-Feb-13 8:46am
v2
Comments
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.

Use a Select Case statement:
VB
Select Case myTextBox.Text
	Case "1234"
		Console.WriteLine("1")
		Exit Select
	Case "2345"
		Console.WriteLine("2")
		Exit Select
End Select
 
Share this answer
 
Comments
vidkaat 22-Feb-13 14:47pm    
I dont want to go for a switch case as all the checks has the same error message.
[no name] 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.
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.
 
Share this answer
 
Comments
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    
Dave,

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.

Thanks.
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)) ...
Use the Contains of the Text property like so:


VB
For Each code As String In validCodes
	If txtSICCode.Text.Contains(code) Then
		DoSomething()
	End If
Next
 
Share this answer
 
v2
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900