locked
Any javascript validation to restrict specific email domain names from entering? RRS feed

  • Question

  • User-851246726 posted

    I get many registrations on my site from users who use disposable mailboxes just so that they can download free stuff.

    Is there any way i can add some kind of javascript validation on the register page email textbox so that it prevents users to type specific email domains?

    This way i can block users from entering disposable mailboxes.

    My email input field looks like this:

    <input class="text-box single-line" data-val="true" data-val-email="Wrong email" data-val-required="Email is required." id="Email" name="Email" type="text" value="">

    Can anyone please help me with a javascript in which i can define my blog email domain names and it gives an error to user if anyone type those domain names?

    Tuesday, November 29, 2016 1:08 PM

All replies

  • User527778624 posted

    Hi,

    Check this code:

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
    <script>
    var domains = ["google","msn","yahoo"]; //update ur domains here
    
    function validateDomain(me){
      var idx1 = me.value.indexOf("@");
      if(idx1>-1){
        var splitStr = me.value.split("@");
        var sub = splitStr[1].split(".");
        if(domains.indexOf(sub[0])>-1){
          me.value="";
          alert("invalid email");
        }
      }
    }
    </script>
    </head>
    <body>
    <input class="text-box single-line" data-val="true" data-val-email="Wrong email" data-val-required="Email is required." id="Email" name="Email" type="email" onblur="validateDomain(this)">
    </body>
    </html>

    or, check demo here: https://jsbin.com/nupoyurudi/edit?html,js,output

    Tuesday, November 29, 2016 2:21 PM
  • User-851246726 posted

    Thank you so much for the code.

    Just  need an advise - do you think this is the best approach from restricting users from using disposable mailboxes and not their valid emails?

    Tuesday, November 29, 2016 2:45 PM
  • User-851246726 posted

    I am getting error:

    Parser Error 
      Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 
    
     Parser Error Message: "");" is not valid at the start of a code block.  Only identifiers, keywords, comments, "(" and "{" are valid.
    
    
    Source Error: 
    
    
    
    Line 16:   var idx1 = me.value.indexOf("@");
    Line 17:   if(idx1>-1){
    Line 18:     var splitStr = me.value.split("@");
    Line 19:     var sub = splitStr[1].split(".");
    Line 20:     if(domains.indexOf(sub[0])>-1){
     
    

    Using this code as-is (on asp. mvc view):

    <script>
    var domains = ["google","msn","yahoo"]; //update ur domains here
    
    function validateDomain(me){
      var idx1 = me.value.indexOf("@");
      if(idx1>-1){
        var splitStr = me.value.split("@");
        var sub = splitStr[1].split(".");
        if(domains.indexOf(sub[0])>-1){
          me.value="";
          alert("invalid email");
        }
      }
    }
    </script>

    Tuesday, November 29, 2016 2:48 PM
  • User527778624 posted

    Hi,

    I am getting error:

    MVC Razor engine tries to parse the words starting with @, so in your case you have to escape @.

    try using @@ instead of @.

    Tuesday, November 29, 2016 3:06 PM
  • User-851246726 posted

    This is not validating anything :(

    <script>
    var domains = ["google","msn","yahoo"]; //update ur domains here
    
    function validateDomain(me){
      var idx1 = me.value.indexOf("@@");
      if(idx1>-1){
        var splitStr = me.value.split("@@");
        var sub = splitStr[1].split(".");
        if(domains.indexOf(sub[0])>-1){
          me.value="";
          alert("invalid email");
        }
      }
    }
    </script>

    Tuesday, November 29, 2016 3:10 PM
  • User527778624 posted

    Hi,

    Please check my code again, your input control need to be added with:

    onblur="validateDomain(this)"
    Tuesday, November 29, 2016 3:11 PM
  • User-851246726 posted

    I am sorry for the incorrect info but the register textbox is actually like this in my view (below) - i provided you the inspected html by mistake.

    <div class="inputs">
                            @Html.LabelFor(model => model.Email, new { }, ":")
                            @Html.EditorFor(model => model.Email)
                            @Html.RequiredHint()
                            @Html.ValidationMessageFor(model => model.Email)
                        </div>

    Tuesday, November 29, 2016 3:18 PM
  • User527778624 posted

    Hi,

    If you are using jquery, add this extra function:

    $(function () {
        $('#Email').blur(function (ele) {
               validateDomain(ele);
        });
    });
    

    check the site for code and demo: https://jsbin.com/nupoyurudi/edit?html,js,output

    Tuesday, November 29, 2016 3:30 PM
  • User-851246726 posted

    Hi Raju,

    In the view, I only have this:

    <div class="inputs">
                            @Html.LabelFor(model => model.Email, new { }, ":")
                            @Html.EditorFor(model => model.Email)
                            @Html.RequiredHint()
                            @Html.ValidationMessageFor(model => model.Email)
                        </div>

    In your example, you are using this which I don't have in the view page so way to add onblur in html for me.

    <input class="text-box single-line" data-val="true" data-val-email="Wrong email" data-val-required="Email is required." id="Email" name="Email" type="email" onblur="validateDomain(this)">

    Tuesday, November 29, 2016 5:05 PM
  • User527778624 posted

    Hi,

    In your example, you are using this which I don't have in the view page so way to add onblur in html for me.

    Just blindly copy/paste this function below validateDomain() function and give it a try.

    $(function () {
        $('#Email').blur(function (ele) {
               validateDomain(ele);
        });
    });

    Tuesday, November 29, 2016 6:15 PM
  • User-851246726 posted

    Ok so I create a separate JS file with this:

    var domains = ["google", "msn", "yahoo"];
    
    function validateDomain(me) {
        var idx1 = me.value.indexOf("@");
        if (idx1 > -1) {
            var splitStr = me.value.split("@");
            var sub = splitStr[1].split(".");
            if (domains.indexOf(sub[0]) > -1) {
                me.value = "";
                alert("invalid email");
            }
        }
    }
    
    $(function () {
        $('#Email').blur(function (ele) {
            validateDomain(ele);
        });
    });
    

    Using this as reference in my view:

    <script src="~/Scripts/emailrestrcition.js"></script>


    I get error:

    Error: Unable to get property 'indexOf' of undefined or null reference

    Tuesday, November 29, 2016 6:31 PM
  • User-851246726 posted

    Hi raju, any help plz?

    I have tried your code several time in my view page but it is still not working?

    Wednesday, November 30, 2016 4:24 AM
  • User-851246726 posted

    I even tried inspecting the page so that I can provide you as much info - see below:

    Wednesday, November 30, 2016 4:28 AM
  • User1724605321 posted

    Hi jeffcarter,

    "me.value" is undefined ,Please modify your code as:

        $(function () {
            $('#Email').blur(function () {
        
                validateDomain($('#Email').val());
            });
        });
        var domains = ["google", "msn", "yahoo"];
    
        function validateDomain(me) {
            debugger;
            var idx1 = me.indexOf("@@");
            if (idx1 > -1) {
                var splitStr = me.split("@@");
                var sub = splitStr[1].split(".");
                if (domains.indexOf(sub[0]) > -1) {
                    $('#Email').val("");
                    alert("invalid email");
                }
            }
        }

    Best Regards,

    Nan Yu

    Wednesday, November 30, 2016 7:18 AM