Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding invalid characters in a string

Status
Not open for further replies.

EvolMonster

Programmer
Aug 14, 2006
41
GB
hiya Guys

For some reason, I can't for the life of me work out how to check for certain characters in a string.

I have to check the email addresses that i'm storing, only have letters or numbers (and an @ symbol).
So far I am looping through each character, and I need to check that each character is valid.

I thought that I could use something like [0-9], but that doesn't seem to work.
Any ideas?

Thanks
DB
 
Having reference to MS VBScript Regular Expressions (5.5):
Code:
Dim RegEx As VBScript_RegExp_55.RegExp
Dim EmailAddressPattern As String
Dim EmailTestedString As String

Set RegEx = New VBScript_RegExp_55.RegExp
EmailAddressPattern = "^([a-z0-9_\.\-]+)\@(([a-z0-9\-]+\.)+)([a-z]+)$"
With RegEx
    .MultiLine = False
    .Global = False
    .IgnoreCase = True
    .Pattern = EmailAddressPattern
End With

EmailTestedString = "string1.string2@address.com"
MsgBox RegEx.Test(EmailTestedString)


combo
 
Sorry, Don't know what that is!!!

I am using VB6...

Can you explain what the above is?!
Is that pattern what I need to compare my email address against?

Thanks
DB
 
The code above (VB) tests validity of string as e-mail address. Requires a reference to 'Microsoft VBScript Regular Expressions 5.5' library. If you need more strict address rules, remove unnecessary characters from the pattern string.

combo
 
I have sorted this now.
I simply use :

If sCharacter Like "[A-Za-z( )] Then
CheckEmailAddress = True
Else
CheckEmailAddress = False
Exit Function
End If

That seems to work.
Thanks anyway combo!

DB
 
EvolMonster,

I would encourage you to take another look at the advice given to you by combo.

email addresses are not limited to letters and numbers. I know that the underscore ( _ ) and the dot (.) are legitimate symbols that can be embedded within an email address.

combo said:
Requires a reference to 'Microsoft VBScript Regular Expressions 5.5' library.

To do this in VB6....

Click Project -> References
scroll down to 'Microsodt VBScript Regular Expressions 5.5'
Select it
Click OK.

You will then be able to use the code provided by combo.

-George

"the screen with the little boxes in the window." - Moron
 
Evol, I've looked a bit at your code, and it seems to allow only upper and lowercase alpha characters and the space. It doesn't allow the @ sign. Furthermore, as stated it won't compile. So, I have trouble understanding the means by which you draw the conclusion that it seems to work, unless the code that you posted here is not the code that you're actually using.

Combo's solution uses Regular Expressions, a mysterious and powerful means to do checks on strings to see if they meet complex criteria. The "valid email" check is well-known and complex. This link should demonstrate the non-triviality of email validation, and should point out a (very) large number of possibilities that you haven't yet considered.

HTH

Bob
 
Hiya Guys

Thanks for the replies. You are right, I didn't include the whole function, so below is it in its entirety :

Private Function CheckEmailAddress(sEmailAddress As String) As Boolean

Dim sCharacter As String
Dim iTextLength As String
Dim iEmailLength As Integer
Dim i As Integer

On Error GoTo ErrorTrap

'Set the check to false before we start
CheckEmailAddress = False

'Set length of the email address
iEmailLength = Len(sEmailAddress)

'Loop through email address checking each character
For i = 1 To iEmailLength
'Set the character value, this gets incremented on each loop

sCharacter = Mid(sEmailAddress, i, 1)

'If the character is valid, set the check to successful, and move onto next character

If sCharacter = "@" Or sCharacter = "-" Or sCharacter = "." _
Or sCharacter = "_" Or sCharacter Like "[A-Za-z( )]" Or sCharacter Like "[0-9( )]" Then
CheckEmailAddress = True

Else
'if an invalid character is found, set the check to false, and exit the function
CheckEmailAddress = False
Exit Function

End If

Next i


ErrorTrap:
blah blah
End Function

If you spot anything that looks rubbish, please let me know!
Thanks
DB
 
So ")@." is a legitimate email address? And ")("?. or "@@@@@@@@@@@@@@@@@@@"?

This is why the others are suggesting more sophisticated pattern matches
 
Sorry, I should have explained this a little more...

These email addresses are coming from our website, which has validation on the email address.
It checks that the address has a certain format - i.e. test@test.com

What it doesn't do is stop email addresses with invalid characters - i.e. test$%@test.com

So this function will check for those invalid characters. I don't really need anything more complex.

DB
 
Because i am assuming you may be new to setting up a reference in your project
Here is a walk through to make the above recommendations easy.

Step 1) Open a new project as usual

Step 2) click the word "Project" on the toolbar at the top of your screen

Step 3) Look to the bottom of the drop down and you will see "References" click this

Step 4) Now scroll the list that appears until you see "Microsoft VBScript Regular Expressions 5.5" place a check in its checkbox

Step 5) Add a commandbutton and a textbox to your project

Step 6) Paste code below

Step 7) Type various email addresses and click test. valid emails will return true


Code:
Public Function TestEmailValidity(StrToTest As String) As Boolean
    Dim Myregex As RegExp
    Set Myregex = New RegExp
    
    
    Myregex.Global = False
    Myregex.IgnoreCase = True
    
    'set a pattern for the regular expression to search for
    Myregex.Pattern = "^([a-z0-9_\.\-]+)\@(([a-z0-9\-]+\.)+)([a-z]+)$"
    
    'test the string
    TestEmailValidity = Myregex.Test(StrToTest)
End Function

Private Sub Command1_Click()
    MsgBox TestEmailValidity(Text1.Text)
End Sub

Private Sub Form_Load()
    Command1.Caption = "Test"
    Text1.Text = "youremail@someisp.com"
End Sub
 
There we go! That extra info helps a lot in putting your question into context.
 
<It checks that the address has a certain format - i.e. test@test.com

This is what strongm was refering to I think.
if you enter @. using the example you provided it would test true.

I think you should give the recommendations on regex a try you will be pleased
 
Sorry yeah, should have mentioned all that. My Bad guys!

And three57m - Thanks for the walkthrough!
I can't really add new references though, as mentioned, because this binary is part of a larger architecture, and I don't want any extra references messing up anything.

Cheers.
DB
 
<this binary is part of a larger architecture, and I don't want any extra references messing up anything.

Huh?

<if you enter @. using the example you provided it would test true

I think the OP is saying that "@." would be filtered out prior to entering the context of his problem.

 
<I think the OP is saying that "@." would be filtered out prior to entering the context of his problem.

Yep i got that after my submit click OOPS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top