0

I have seen a similar question asked here Jquery how to validate domain name but I cannot seem to get this example to work with my code.

I would basically like to perform a reg ex against an input text box, and validate via jquery to see if it matches my search criteria for a domain name. The format must be

domainname.co.uk or .com/.org/.net (no http:// or https:// and no subdomain)

My code currently :

if(!/([a-z0-9-]+\.(?:com|net|org|co.uk))(?:\/|$)/$("#domain").val()) {
        $('#error').html("Invalid domain name, , please correct before proceeding.");
        return false;   
}
Community
  • 1
  • 1
Graeme Leighfield
  • 2,825
  • 3
  • 23
  • 38

1 Answers1

4
!/([a-z0-9-]+\.(?:com|net|org|co.uk))(?:\/|$)/$("#domain").val())

Should be:

!/([a-z0-9-]+\.(?:com|net|org|co\.uk))(?:\/|$)/.test($("#domain").val()))
//                              ^              ^^^^^
gdoron
  • 147,333
  • 58
  • 291
  • 367
  • Thanks so much. Sometimes a fresh set of eyes is the simplest solution! Will accept as answer when initial lock has gone :) – Graeme Leighfield Nov 19 '12 at 22:23
  • 1
    @GraemeLeighfield. I know the feeling, had the same thing today with SQL, take a coffee break. – gdoron Nov 19 '12 at 22:24
  • gdoron, what do i need to ammend in the regex to not allow http://www. as it doesnt seem to do that at the moment? – Graeme Leighfield Nov 19 '12 at 22:26
  • 1
    @GraemeLeighfield, since javascript doesn't have negative lookbehind, I would have use `indexOf`. [Read here for more info](http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) – gdoron Nov 19 '12 at 22:33