Validate fake email addresses using Regular Expression and Custom Validator in ASP.Net

Last Reply on Oct 13, 2014 05:35 AM By Azim

Posted on Oct 11, 2014 11:39 PM

Hi, 

i am working in asp.net 4.5 and i am working on registration page. i have email field on my page i am using below regex for check email validation.

 

<asp:TextBox ID="txtEmailAddress" runat="server" ValidationGroup="Registration" autocomplete="off" />
 <asp:RequiredFieldValidator ID="reqfvUsername" runat="server" ControlToValidate="txtEmailAddress"
                                            ErrorMessage="Field Required" Display="Dynamic" ValidationGroup="Registration" ForeColor="Red">
 <asp:RegularExpressionValidator ID="regexEmailAddress" runat="server" ValidationGroup="Registration" ForeColor="Red"
                                            ErrorMessage="Invalid email address" ControlToValidate="txtEmailAddress"
                                            ValidationExpression="(?!(alpha|beta|ALPHA|BETA)@email\.com|email@(msn|yahoo|MSN|YAHOO)\.com|(testing|TESTING)@gmail\.com|.*@(yesmail|YESMAIL)\.com)^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"
 Display="Dynamic">
<asp:Button ID="btnRegister" runat="server" Text="Register" ValidationGroup="Registration" />

for example, if the email contains

alpha@email.com 

beta@email.com

email@yahoo.com

email@msn.com 

testing@gmail.com

then i want to throw invalid email address message. can it be done using regular expression? please show me some sample code.

this is working fine for the lowe case. for uppercase /mixed case its not working. how can i make it to case insensitive? any suggestions or samples please

Posted on Oct 13, 2014 05:35 AM

Please refer this code

HTML

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtEmail" runat="server" />
    <asp:Button Text="Save" runat="server" />
    <asp:CustomValidator ControlToValidate="txtEmail" runat="server" ClientValidationFunction="ValidateEmailAddress" />
    <script type="text/javascript">
        function ValidateEmailAddress(source, args) {

            args.IsValid = true;
            var expr = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
            var email = document.getElementById(source.controltovalidate).value.replace(/^\s+|\s+$/g, '');

            if (expr.test(email)) {
                switch (email) {
                    case "alpha@email.com":
                        source.innerHTML = "Please try a different Email Address";
                        args.IsValid = false;
                        break;
                    case "beta@email.com":
                        source.innerHTML = "Please try a different Email Address";
                        args.IsValid = false;
                        break;
                    case "email@yahoo.com":
                        source.innerHTML = "Please try a different Email Address";
                        args.IsValid = false;
                        break;
                    case "email@msn.com":
                        source.innerHTML = "Please try a different Email Address";
                        args.IsValid = false;
                        break;
                    case "testing@gmail.com":
                        source.innerHTML = "Please try a different Email Address";
                        args.IsValid = false;
                        break;
                }
            }
            else {
                source.innerHTML = "Invalid Email Address";
                args.IsValid = false;
            }
        }
    </script>
</div>
</form>

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html