Solved

Check Domain Name Syntax with JS

Posted on 2007-03-18
1
193 Views
Last Modified: 2008-03-31
Hello,

I would like to use JavaScript to determine the syntax validity of a domain name. This will be used in an internal Intranet, so in addition to all standard domain name extensions, other internal extensions should be allowed as well.

1. There should be at least 1 dot.
2. Only letters, numbers and dashes should be allowed before the LAST dot.
3. Only letters should be allowed AFTER the LAST dot.
4. This should be case insensitive

All of these should PASS
        www.example.com
        www.example.intranet
        sub.portal.example.intranet
        example-web-page-123.mobi
        2534457.2343.com
        234-64.23.112.intrra
        234-64.23.112.2323.5234.34323.43234.234.net


But all of these should FAIL:
        example  com
        example@example.com
        sub_portal.example.intranet
        example-web-page-123
        192.168.0.1





This does NOT work.  Any ideas how to fix it?
Thanks!

<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
function isValidDomain(obj){
    var urlPattern = /^(?:(?:[-a-z0-9]*[a-z0-9])?\.)+([a-z])?/;
      return urlPattern.test(obj.value);      
      }

</script>
</head>
<body>
<form name="frm1" method="post" action="" onsubmit="return ">
<input type="text" name="txt1" value="webaddress.com">
<input type="button" value="Valid Domain?" onclick="alert(isValidDomain(this.form.txt1))">
</form>
</body>
</html>
0
Comment
Question by:hankknight
1 Comment
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18743956
Use this regular expression:

/^[A-Za-z0-9\-\.]*\.[A-Za-z]*$/
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 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