How can i test a text from a textbox containing only one "@" ?

VB.NET:
Dim str As String = myTextBox.Text
Dim firstIndex As Integer = str.IndexOf("@"c)
Dim lastIndex As Integer = str.LastIndexOf("@"c)

If firstIndex <> -1 AndAlso firstIndex <> lastIndex Then
    'There is one and only one "@" in the TextBox.
End If
 
It seems u know all the properties of a string variable. This code is too advanced for me right now. :) In the future it won`t be. Thx anyway.

All its doing, is make a variable called "str" and type "string", its value = textbox1.text .

Basically, if you can find the character number of the first and last "@" , you can compare them both.

If the first "@" location is the same as the last, theres only one.

example 1 "test@12@45@hello.com" first= 5 last = 11
example 2 "12345678@101112" first= 9 last = 9

(im not sure if it counts 0 as the first..sorry).



I expect -1 is what is returned if there is 0 @'s found.

If -1 "none found"
if first and last are the same "1 found"
if first and last different "more than 1 found"

Hope that helps a little! :)
 
How can i test a text from a textbox containing only one "@" ?


Test it for what? What contains one @? The text, or the textbox?

VB.NET:
If "@".Equals(textbox1.Text) Then
  'the box contains one @
Else
  'the box contains soemthing else
Endif
 
I'm guessing that the intention here is to validate an e-mail address, thus one and only one "@" symbol is required. There's quite a bit more to properly validating an e-mail address than that though. It really depends how much you want to rely on the user and how much you want to hold their hand. There are numerous characters that are invalid in e-mail addresses, including spaces. Also, it's not valid to start or end with a dot. Generally the "easiest" way is to use a regular expression. I've got a book on regular expressions that gives an example of a regex that validates e-mail addresses and it's more than a page long. That's an idea of how difficult it is to validate an e-mail address fully. You can do an incomplete but fairly thorough job with something much shorter than that though. Here's some code I use to validate an e-mail address field and set an ErrorProvider if it's invalid:
VB.NET:
If Me.emailText.Text.Trim() = String.Empty OrElse _
   Regex.IsMatch(Me.emailText.Text, My.Resources.EmailRegexPatternString) Then
    Me.errorIndicator.SetError(Me.emailText, Nothing)
Else
    Me.errorIndicator.SetError(Me.emailText, My.Resources.InvalidEmailErrorString)
End If
My.Resources.EmailRegexPatternString has the following value:

^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
 
LeonR thx for your explanation. I didn`t know that IndexOf or LastIndefOf were doing. C in the code stands for Character right ? :)


Cjard, jmcilhinney supposed correctly. I wanted to valid an e-mail address and i can see that it`s a complex way to test it. I only wanted to search for de "@" caracter but it`s more than this. I`m only trying to learn the basics here .. then i`ll study more complex codes & i`ll try to make my own applications.. Hope i`ll succed in 5 or 6 months.
 
Hi,
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] TextBox1.Text.Contains([/SIZE][SIZE=2][COLOR=#800000]"@"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]    MsgBox([/SIZE][SIZE=2][COLOR=#800000]"Yes, TextBox1 Contains '@'"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]    MsgBox([/SIZE][SIZE=2][COLOR=#800000]"No, TextBox1 not Contains '@'"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/COLOR][/SIZE]
 
Hi,
VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] TextBox1.Text.Contains([/SIZE][SIZE=2][COLOR=#800000]"@"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]    MsgBox([/SIZE][SIZE=2][COLOR=#800000]"Yes, TextBox1 Contains '@'"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE]
[SIZE=2]    MsgBox([/SIZE][SIZE=2][COLOR=#800000]"No, TextBox1 not Contains '@'"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[/COLOR][/SIZE]
Nice try but the question was how to determine whether the TextBox contains ONLY one "@". That code tests for the presence of one OR MORE "@"s
 
Hi,

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] n, n1 [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] n = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] Len(TextBox1.Text) - 1[/SIZE]
[SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] (TextBox1.Text(n)) = [/SIZE][SIZE=2][COLOR=#800000]"@" [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]     n1 += 1[/SIZE]
[SIZE=2][COLOR=#0000ff] End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2]MsgBox([/SIZE][SIZE=2][COLOR=#800000]"TextBox1 Contains "[/COLOR][/SIZE][SIZE=2] & n1)[/SIZE]
 
Back
Top