Solved

Check Domain Name Syntax with JS

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Debugging step-by-step Jquery/Javascript 6 57
What is assert.deepEqual? 4 39
Add Context Menu Item 4 43
Add css and js to MVC Project not in project 9 27
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now