Solved

Check Domain Name Syntax with JS

Posted on 2007-03-18
1
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Read text on Table 7 49
Problem in javascript 17 58
Geolocation works...sometimes 2 39
Why am I getting SyntaxError: missing ; before statement error 39 80
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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