Solved

RegExp and Anonymous Email check help

Posted on 2002-03-07
20
222 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now