Solved

RegExp and Anonymous Email check help

Posted on 2002-03-07
20
224 Views
Last Modified: 2008-02-26
Hi. Please tell me how I can solve this problem.

I have a form with a textbox for an email address. I would like to take existing code for a valid email check, and add a check for anonymous email hosts (e.g. hotmail, msn, yahoo, aol, etc). It already returns an error string correctly, but I'd like to add that host check too.

Is it possible to do this? Somebody wrote the code for me, so I don't know completely what it does:

-------    ---    -------
<!-- #include file="emailchk.asp" -->

Function RegExpTest(patrn, strng)
     RegExpTest = True
     if len(patrn) = 0 then Exit Function

     Dim regEx, Match, Matches                    ' Create variable.
     Set regEx = New RegExp                    ' Create a regular expression.
     regEx.Pattern = patrn                         ' Set pattern.
     regEx.IgnoreCase = False                    ' Set case sensitivity.
     regEx.Global = True                         ' Set global applicability.
     Set Matches = regEx.Execute(strng)     ' Execute search.
     if Matches.Count > 0 then RegExpTest = False
End Function

Function CheckStringField( ByRef strField, intMinLen, intMaxLen, strRegExpPattern )
     Dim intLen
     strField = Trim( strField )
     intLen = len( strField )
     if intLen = 0 and intMinLen > 0 then
          CheckStringField = "-cannot be empty!"
     elseif intLen < intMinLen then
          CheckStringField = "-too short!"
     elseif intLen > intMaxLen then
          CheckStringField = "-too long!"
     elseif Not RegExpTest( strRegExpPattern, strField ) then
          CheckStringField = "-invalid!"
     else
          CheckStringField = ""
     end if
End Function

--------
...located in the asp page that has the form:

strErrEM  = CheckStringField( strEM,  5, 50, strBad )
--------------------------

Thanks very much!
0
Comment
Question by:4ad
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6847671
This should give you an idea how to catch the email provider:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function getPublicEmail()
{
strEmail = document.form1.text1.value
iStartPosition = strEmail.indexOf("@")
iEndPosition  = strEmail.lastIndexOf(".")
strProvider = strEmail.substring(iStartPosition + 1,iEndPosition)
alert(strProvider)

}
//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=form1 name=form1><P>&nbsp;</P>
Email Address<INPUT type="text" id=text1 name=text1 onChange="javascript:getPublicEmail()">
</FORM>
</BODY>
</HTML>
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6847699
Here is a slightly better version doing what you asked for:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function getPublicEmail()
{
     strEmail = document.form1.text1.value
     iStartPosition = strEmail.indexOf("@")
     iEndPosition  = strEmail.lastIndexOf(".")
     strProvider = strEmail.substring(iStartPosition + 1,iEndPosition)
     strProvider = strProvider.toUpperCase()
     if(strProvider=="HOTMAIL" || strProvider=="YAHOO" || strProvider == "AOL" || strProvider=="MSN"){
          alert("Public address: " + strProvider);
          return false;
     }
}
//-->
</SCRIPT>


</HEAD>
<BODY>

<FORM action="" method=POST id=form1 name=form1><P>&nbsp;</P>
Email Address<INPUT type="text" id=text1 name=text1 onChange="javascript:getPublicEmail()">
</FORM>
</BODY>
</HTML>


Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6848723
Sorry,

I just realized that you probably want this in VB not JavaScript. Here 'tis:

function getPublicEmail()
    strEmail = document.form1.text1.value
    iStartPosition = inStr(1,strEmail,"@",1)
    iEndPosition  = len(strEmail) - 4
    strProvider = mid(strEmail,iStartPosition + 1,iEndPosition-iStartPosition)
     if UCase(strProvider) ="HOTMAIL" OR strProvider="YAHOO" OR strProvider = "AOL" OR strProvider="MSN" then
          msgbox strProvider
     end if
end function
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 23

Expert Comment

by:b1xml2
ID: 6849604
<html>
<head>
<title>Mail Reg Exp Client-Side Validation</title>
<script language="javascript">
var regMail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i;
var regHost = /(yahoo|hotmail|msn|aol)/i;
function validateMail(frm) {
     var szMsg = "";
     if (frm.mail.value.match(regMail)) {
          var szMail = RegExp.$2;
          if (szMail.match(regHost)) szMsg = "Hotmail, Yahoo, MSN and AOL Accounts Are Not Accepted.";
     } else {
          szMsg = "Invalid Email Format.";
     }
     if (szMsg.length == 0) return true;
     alert("Validation Of EMail Address Failed\n================================\n" + szMsg);
     return true;
     
}


</script>
</head>
<body bgcolor="white">
<form onsubmit="return validateMail(this);">
<input type="text" size="50" maxlength="255" name="mail">
<input type="submit" value="Submit">
</form>
</body>
</html>
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6849605
repost, wrong return value at the end of the function

<html>
<head>
<title>Mail Reg Exp Client-Side Validation</title>
<script language="javascript">
var regMail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i;
var regHost = /(yahoo|hotmail|msn|aol)/i;
function validateMail(frm) {
     var szMsg = "";
     if (frm.mail.value.match(regMail)) {
          var szMail = RegExp.$2;
          if (szMail.match(regHost)) szMsg = "Hotmail, Yahoo, MSN and AOL Accounts Are Not Accepted.";
     } else {
          szMsg = "Invalid Email Format.";
     }
     if (szMsg.length == 0) return true;
     alert("Validation Of EMail Address Failed\n================================\n" + szMsg);
     return false;
     
}


</script>
</head>
<body bgcolor="white">
<form onsubmit="return validateMail(this);">
<input type="text" size="50" maxlength="255" name="mail">
<input type="submit" value="Submit">
</form>
</body>
</html>
0
 

Author Comment

by:4ad
ID: 6850373
Yes, VBScript is preferred.

Fritz, please help me understand your code.
- (for my sake) can you explain what/how it's checking?
- what is the msgbox strProvider? How do I use that?
- do I need the onChange somewhere to make it work?

Thanks!
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6850449
Fair enough; here is the code with comments:

function getPublicEmail()
     'Get the value of the email addres    
     strEmail = document.form1.text1.value
     'Find the position of the @ symbol in the email address
     iStartPosition = inStr(1,strEmail,"@",1)
     'find the position of the "." in .com, .net, and etc.
     iEndPosition  = len(strEmail) - 4
     'Find the portion of the string between the "@" and the "."
     strProvider = mid(strEmail,iStartPosition + 1,iEndPosition-iStartPosition)
     'Determine if the provider is Yahoo, hotmail, AOL and etc,
      if UCase(strProvider) ="HOTMAIL" OR strProvider="YAHOO" OR strProvider = "AOL" OR strProvider="MSN" then
          'you probably want to get rid of this, it is just to show you that the function works
          'You should do whatever processing you want here.
          msgbox strProvider
     end if
end function
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6850460
Here is how to encorporate it into your code:

function getPublicEmail()
     'Get the value of the email addres    
     strEmail = document.form1.text1.value
     'Find the position of the @ symbol in the email address
     iStartPosition = inStr(1,strEmail,"@",1)
     'find the position of the "." in .com, .net, and etc.
     iEndPosition  = len(strEmail) - 4
     'Find the portion of the string between the "@" and the "."
     strProvider = mid(strEmail,iStartPosition + 1,iEndPosition-iStartPosition)
     'Determine if the provider is Yahoo, hotmail, AOL and etc,
      if UCase(strProvider) ="HOTMAIL" OR strProvider="YAHOO" OR strProvider = "AOL" OR strProvider="MSN" then
          getPublicEmail = strProvider
     else
          getPublicEmail= ""
     end if
end function

Function CheckStringField( ByRef strField, intMinLen, intMaxLen, strRegExpPattern )
    Dim intLen
    strField = Trim( strField )
    intLen = len( strField )
    if intLen = 0 and intMinLen > 0 then
         CheckStringField = "-cannot be empty!"
    elseif intLen < intMinLen then
         CheckStringField = "-too short!"
    elseif intLen > intMaxLen then
         CheckStringField = "-too long!"
    elseif Not RegExpTest( strRegExpPattern, strField ) then
         CheckStringField = "-invalid!"
    else
         CheckStringField = ""
    end if
     CheckStringField = getPublicEmail()
End Function
0
 

Author Comment

by:4ad
ID: 6850489
fritz,...well, I added the code to the page. No errors, but it's not writing the error back to the user.

In my code it's returned as <%=strErrEM%> in the form <td>.

I'm confused where this can do it in the code you provided?

Thanks very much.
0
 

Author Comment

by:4ad
ID: 6850492
oops, page didn't refresh before I posted again. Thanks for the explanation. I'll try it right now!
0
 

Author Comment

by:4ad
ID: 6850609
hmm, now it's giving me this error:

Variable is undefined: 'document'

Once I dimmed it, all sorts of errors started popping up.

??
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6850619
Sorry! That's my mistake. Please give me a few minutes to correct it.

Fritz the Blank
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6850625
Okay, try this:

function getPublicEmail(strField)
     'Get the value of the email address and remove leading/trailing spaces    
     strEmail = trim(strField)
     'Find the position of the @ symbol in the email address
     iStartPosition = inStr(1,strEmail,"@",1)
     'find the position of the "." in .com, .net, and etc.
     iEndPosition  = len(strEmail) - 4
     'Find the portion of the string between the "@" and the "."
     strProvider = mid(strEmail,iStartPosition + 1,iEndPosition-iStartPosition)
     'Determine if the provider is Yahoo, hotmail, AOL and etc,
      if UCase(strProvider) ="HOTMAIL" OR strProvider="YAHOO" OR strProvider = "AOL" OR strProvider="MSN" then
          getPublicEmail = strProvider
     else
          getPublicEmail= ""
     end if
end function

Function CheckStringField( ByRef strField, intMinLen, intMaxLen, strRegExpPattern )
    Dim intLen
    strField = Trim( strField )
    intLen = len( strField )
    if intLen = 0 and intMinLen > 0 then
         CheckStringField = "-cannot be empty!"
    elseif intLen < intMinLen then
         CheckStringField = "-too short!"
    elseif intLen > intMaxLen then
         CheckStringField = "-too long!"
    elseif Not RegExpTest( strRegExpPattern, strField ) then
         CheckStringField = "-invalid!"
    else
         CheckStringField = ""
    end if
     CheckStringField = getPublicEmail(strField)
End Function
0
 

Author Comment

by:4ad
ID: 6850748
Still not liking me. It returns

Variable is undefined: 'strEmail'

So, I Dim strEmail, iStartPosition, iEndPosition, and strProvider

then it says:

Invalid procedure call or argument: 'Mid'

(All of this code is in an include at the top of the .asp page with the form. Is that making a difference for me?)
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 100 total points
ID: 6850807
Okay,

Are you sure that you are passing a valid email address?

Here is the code with protection against an invalid email:

function getPublicEmail(strField)
     'Get the value of the email address and remove leading/trailing spaces    
     strEmail = trim(strField)
     'Make sure that the email address is at least 6 characters long
     if len(strEmail)>5 then
          'Find the position of the @ symbol in the email address
          iStartPosition = inStr(1,strEmail,"@",1)
          'find the position of the "." in .com, .net, and etc.
          iEndPosition  = len(strEmail) - 4
          'Find the portion of the string between the "@" and the "."
          strProvider = mid(strEmail,iStartPosition + 1,iEndPosition-iStartPosition)
          'Determine if the provider is Yahoo, hotmail, AOL and etc,
           if UCase(strProvider) ="HOTMAIL" OR strProvider="YAHOO" OR strProvider = "AOL" OR strProvider="MSN" then
               getPublicEmail = strProvider
          else
               getPublicEmail= ""
          end if
     else
          getPublicEmail= "email address is too short"
     end if
end function

Function CheckStringField( ByRef strField, intMinLen, intMaxLen, strRegExpPattern )
    Dim intLen
    strField = Trim( strField )
    intLen = len( strField )
    if intLen = 0 and intMinLen > 0 then
         CheckStringField = "-cannot be empty!"
    elseif intLen < intMinLen then
         CheckStringField = "-too short!"
    elseif intLen > intMaxLen then
         CheckStringField = "-too long!"
    elseif Not RegExpTest( strRegExpPattern, strField ) then
         CheckStringField = "-invalid!"
    else
         CheckStringField = ""
    end if
     CheckStringField = getPublicEmail(strField)
End Function
0
 

Author Comment

by:4ad
ID: 6851071
wow, I can't figure this one out yet. I must not have the email address being passed correctly at all. Other things happening too I need to look at.

Your getPublicEmail= "email address is too short"  comes up, and it somehow blow past parts of the original function and neglects the rest of the checks throughout the form.

...still working...
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6851174
At some point, you should have something like:

strEmail = Request.Form("email")


Fritz the Blank
0
 

Author Comment

by:4ad
ID: 6896985
Hello experts. I am sorry to say that I have been stuck on this problem for too long. I need to find a different workaround, and have notified community support. Thus, I have decided to close this question and split the 200 between fritz and b1xml2. Each with A grades. Thanks very much for your excellence.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6898356
Hi, 4ad.  I have refunded 100 points to you so you can now award one of the experts directly in this question and post a new question in this topic area for the other experts.  Please entitle it.... Points for __expertname__ and in the comment field paste the link (URL) to this question.

More detailed information in your Community Support link below.
http://www.experts-exchange.com/questions/Q.20281690.html

Thank you,
Moondancer - EE Moderator
0
 

Author Comment

by:4ad
ID: 6997749
sorry it took me a while
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question