C and C++ Programming Resources

Validate email address in ASP

Technical discussion on Active Server Pages development, also you could request the source code for your website.

Validate email address in ASP

Postby kami » Sun Jul 10, 2011 8:13 pm

Is there a simple way or function to validate email address in ASP?
User avatar
kami
 
Posts: 2
Joined: Thu Aug 19, 2004 1:03 pm
Location: Pakistan

Re: Validate email address in ASP

Postby administrator » Sun Jul 17, 2011 5:25 pm

Code: Select all
Function IsValidEmail(emailAddress)
'Declare variables
Dim ValidEmail, emailParts, iLoopCounter, emailChar, acceptableChars
   ValidEmail = True 'set the default result to True
   'acceptableChars are the characters that we will allow in our email
   acceptableChars="abcdefghijklmnopqrstuvwxyz.-_@"
   'use the Split function to create an array with the @ as the separator
   'so if your email was test@tester.com the email would be split into an array
   'with the first array element holding "test" and the second "tester.com"
   emailParts = Split(emailAddress, "@")
   'check to make sure that there is only 1 @ and that there are 2 parts
   'remember arrays are zero based
   'Using the UBound function will return the highest element in the array
   'So if it's a valid email the UBound function will return 1, i.e. 0 start
   If UBound(emailParts) <> 1 Then
      ValidEmail = false
   Else
      'Check the length of each part of the email address
      'first part can be just one character, 2nd part must be atleast 4
      If Len(emailParts(0))<1 OR Len(emailParts(1))<4 Then
         ValidEmail = false
      End If
      'check first character on the left part isn't a "." using Left function
      If Left(emailParts(0), 1)="." Then
         ValidEmail = false
      End If
      'check the last & 2nd character from right part using Right function
      If Right(emailParts(1), 1) = "." OR Right(emailParts(1), 2) = "." Then
         ValidEmail = false
      End If
      'check that there is a . in the second part of the email address - .com
      If InStr(emailParts(1), ".") <= 0 Then
         ValidEmail = false
      End If
      'check that there shouldn't be a _ in the second part of the email address
      If InStr(emailParts(1), "_") >0 Then
         ValidEmail = false
      End If
   End If
      'loop through each character of email
   For iLoopCounter = 1 to Len(emailAddress)
   'Use Lcase & Mid functions, Mid function used to return each individual character
   'in the email, and then Lcase converts it into lowercase
   emailChar = Lcase(Mid(emailAddress, iLoopCounter, 1))
   'Check if the emailAddress characters are acceptable
      If InStr(acceptableChars, emailChar) = 0 and Not IsNumeric(emailChar) Then
         ValidEmail = false
      End if
   Next
   'check if there is 2 . in a row
   If InStr(emailAddress, "..") > 0 Then
      ValidEmail=false
   End If
   'check if there is @. in a row
   If InStr(emailAddress, "@.") > 0 Then
      ValidEmail=false
   End If
   IsValidEmail=ValidEmail
End function

User avatar
administrator
 
Posts: 90
Joined: Sun Feb 22, 2004 1:42 am
Location: United Kingdom

Next

Return to ASP and ASP .NET Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron