Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check Domain Name Syntax with JS

Posted on 2007-03-18
1
Medium Priority
?
207 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 2000 total points
ID: 18743956
Use this regular expression:

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

730 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