3

I have a input field where users can write a domain name that they wish for search for.

My domain search only support searches as: "something.com" or "something"

And it only support .com, .net, .org

That I want to do is validate the user input before the ajax call is made and display an error message if anything is wrong. Example if a user types: "asdasdasd.asdasd" or "wwwasd.asdasd" or "www.asdasd.dk"

#domainsearch is the input field

#domanerknap is the search button

Here is my Jquery:

$(document).ready(function() {
    $('#domanerknap').click(function(e) {
        var doma = $('#domainsearch').val();
        $('#contentajax').empty()
                         .css("border", "1px solid #CCCCCC")
                         .html('<p class="vent">Please wait...</p><p class="venter"><img src="../images/ajax.gif" /></p>');
        $.ajax({
            type: 'POST',
            url: 'http://localhost:3000/domain',
            data: {
                domain: doma
            },
            success: function(msg){
                $('#contentajax').html(msg);
                $('#contentajax').css("border", "none");
            }
        });        
    });
});
Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
Rails beginner
  • 14,321
  • 35
  • 137
  • 257

3 Answers3

2

Use the pattern as shown below. To construct this pattern, I used the following rules:

  • The domain must not contain a protocol
  • Only alphanumeric characters and hypens are a valid domain name
  • Sub domains are allowed
  • Top level domain must be either of the following: com net org

I have also included a dynamic match, so that protocols and paths are automatically omitted when input.

var domainPattern = /^(https?:\/\/)?((?:[a-z0-9-]+\.)+(?:com|net|org))(?:\/|$)/i
if(doma.match(domainPattern)){
    doma = doma[1]; //Get the domain parth
    $.ajax(...); //Valid
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • @Railsbeginner If you want to select the root domain, use `/([a-z0-9-]+\.(?:com|net|org))(?:\/|$)/i`. This pattern will attempt to select a matching url from a given string, eg `"my site is example.com/mysite"` --> `example.com`. – Rob W Oct 27 '11 at 13:35
  • you should probably escape your `-` sign between the brackets – gion_13 Oct 27 '11 at 13:37
  • @gion_13 A hypen inside square braces don't have to be escaped when it's at the beginning or end of the square braces. @Railsbeginner Using the `/([a-z0-9-]+\.(com|net|org))/` pattern (see answer), you can refer to `example.com` using `doma[1]`, and to `com` using `doma[2]`. – Rob W Oct 27 '11 at 13:41
  • @Rob I example my var doma is "wwwwwwwwww.dddddddd" and I created the alert alert('invalid domain name' + doma[2]); the error is 'invalid domain namew' and not 'invalid domain name.dddddddd' – Rails beginner Oct 27 '11 at 13:45
  • @Railsbeginner To get the original input, use `doma[0]`. – Rob W Oct 27 '11 at 13:54
  • A domain does not include scheme (http, https, ftp etc...). The scheme is a component of the URL/URI – Timothy C. Quinn May 18 '23 at 16:10
0
if( !doma.match(/^[a-z0-9_-]+(\.(com|net|org))?$/i)) { /* error stuff here */
else $.ajax(...);

Something like that, I would imagine, going by the examples of valid input you gave.

You can also put the part between the /.../ in the pattern attribute of the input element, and supporting browsers will match it before even allowing a submission.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • it accepts `dash` at the beginning of the domain name, example: `-new.com` which is obviously wrong... – behz4d Apr 19 '14 at 17:41
0

use a regex to check if the domain is valid. Such a regex might be :

/^(http(s)?\/\/:)?(www\.)?[a-zA-Z\-]{3,}(\.(com|net|org))?$/

Then before you send the ajax call, check to see if the input matches the regex :

$(document).ready(function() {
    $('#domanerknap').click(function(e) {
        var doma = $('#domainsearch').val();
        if(!/^(http(s)?\/\/:)?(www\.)?[a-zA-Z\-]{3,}(\.(com|net|org))?$/.test(doma))
            {
                alert('invalid domain name');
                return false;
            }
        $('#contentajax').empty()
                         .css("border", "1px solid #CCCCCC")
                         .html('<p class="vent">Please wait...</p><p class="venter"><img src="../images/ajax.gif" /></p>');
        $.ajax({
            type: 'POST',
            url: 'http://localhost:3000/domain',
            data: {
                domain: doma
            },
            success: function(msg){
                $('#contentajax').html(msg);
                $('#contentajax').css("border", "none");
            }
        });        
    });
});
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • It says "Invalid domain name" when submitting "asdasdasd" which it should allow how to change that? – Rails beginner Oct 27 '11 at 13:36
  • see update. sry, i missed the fact that you wanted to be able to exclude the domain extension – gion_13 Oct 27 '11 at 13:40
  • How to raise and error example if the var doma is "testsome.oaa" then the error would be ".ooa is not supported or invalid" and if the var doma is "www.adsasd" it would raise the error invalid domain name "http://www.www.adsasd" – Rails beginner Oct 27 '11 at 13:48