[RESOLVED] String Functions in VB
I am making a form at my work. I have a e mail field. I wantto validate it. I was looking for a string function that can take character by character from the given textbox and check for "@" and then "." signs. Going through the help file i found a lot a of functions like substring and others but I couldn't understand how to use it. Can anyone give me a code for the above problem or can tell me in a better way how to use some of the functions.
Thanks Anup
Re: String Functions in VB
Use the IndexOf() function. Basically you will be doing something like this:
If <your string here>.IndexOf("@")>-1 then
'You found one.
End if
Then repeat for any other characters you are looking for.
Re: String Functions in VB
You're better off using a regular expression to validate an e-mail address. Here's a simple one:
VB Code:
If Not System.Text.RegularExpressions.Regex.IsMatch(Me.TextBox1.Text, _
"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") Then
MessageBox.Show("Please enter a valid e-mail address.")
End If
There are more complex regex patterns that do a better job of validating but this one is adequate for most circumstances.
Re: String Functions in VB
thanks guys that things is alread resolved i used the like operator instead but jim that was some cool suff if you can just tell what it means it would be great my email is vishwajeet_th@yahoo.com...........thanks shaggy for your help to that was easier to understand...........anyway guys i got a new problem that i thought i have resolved somehow..........i want to check whether the value entered by the user exists in database or not........as the field entred is primary key so i want to find whther there is a conflict if thee is i want to display a message in the database. i am using the following code but it is not working
If Adodc1.Recordset.Fields(0).Value = txtid.Text Then
MsgBox ("Employee Id already exist in database")
txtid.SetFocus
apart from the above code i have also tried these but it is not working
If Adodc1.Recordset.Find(txtid.Text) Then
MsgBox ("Employee Id already exist in database")
txtid.SetFocus
If Adodc1.Recordset.Fields(0) = txtid.Text Then
MsgBox ("Employee Id already exist in database")
txtid.SetFocus
the last code was the one that i was using and it was working ok before....i know this should not be here i should make a new post or change the title but still if u can help me
thanks anup.
Re: String Functions in VB
Regular expressions have been around for a long time and they are very powerful, but the syntax is such that they are not easy to do anything remotely complex without a bit of practice. If you want more information then there are many sites on the Web devoted to the topic. Below are a couple that I've used in the past.
http://www.regular-expressions.info/
http://gnosis.cx/publish/programming...pressions.html
Re: String Functions in VB
That doesn't look like ADO.NET, is it older ADO? I don't recognize the recordset.
The way I would do it, assuming you have a connection called conn would be something like this:
VB Code:
'First get a command object:
dim cmd as system.data.oledb.oledbCommand = conDB.CreateCommand
dim i as integer
cmd.CommandText = "SELECT ? WHERE ? = ' & txtid.Text & "'"
i = cmd.ExecuteScalar
'See whether anything was returned.
Something like that. Returning a scalar is very fast, faster than a recordset. However, I added the symbol ? which is not valid SQL. Basically, you want to replace that symbol with the field name. Also, this assumes that the field is an integer. It really doesn't matter what field you return, except that it can't be Null. If the row is found, then i will be set to something other than the 0 it starts out with.
On the other hand, this may be a whole new realm, since it looks like a new version of ADO.
I really have to get into regular expressions, they are just too cool.