RegExp and Anonymous Email check help

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!
4adAsked:
Who is Participating?
 
fritz_the_blankConnect With a Mentor Commented:
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
 
fritz_the_blankCommented:
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
 
fritz_the_blankCommented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
fritz_the_blankCommented:
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
 
b1xml2Commented:
<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
 
b1xml2Commented:
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
 
4adAuthor Commented:
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
 
fritz_the_blankCommented:
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
 
fritz_the_blankCommented:
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
 
4adAuthor Commented:
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
 
4adAuthor Commented:
oops, page didn't refresh before I posted again. Thanks for the explanation. I'll try it right now!
0
 
4adAuthor Commented:
hmm, now it's giving me this error:

Variable is undefined: 'document'

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

??
0
 
fritz_the_blankCommented:
Sorry! That's my mistake. Please give me a few minutes to correct it.

Fritz the Blank
0
 
fritz_the_blankCommented:
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
 
4adAuthor Commented:
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
 
4adAuthor Commented:
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
 
fritz_the_blankCommented:
At some point, you should have something like:

strEmail = Request.Form("email")


Fritz the Blank
0
 
4adAuthor Commented:
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
 
MoondancerCommented:
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
 
4adAuthor Commented:
sorry it took me a while
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.