Solved

Check Domain Name Syntax with JS

Posted on 2007-03-18
1
187 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now