javascript email, ip or dns validation

Posted on 2012-08-24
Medium Priority
Last Modified: 2012-08-27
hi, i have the following piece of code to validate an address - either an email, or a dns or even an ip address - allowing % for wildcards.
the regular expression script is below:
var ml = /^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i;
var ip = /^[\d%]{1,3}\.[\d%]{1,3}\.[\d%]{1,3}\.[\d%]{1,3}$/i;
var dn = /^(?![0-9]+$)(?!-)[a-zA-Z0-9%-]{,63}(?<!-)$/i;
if (!ml.test(eml) || !ip.test(eml) || !dn.test(eml)) {
	salert("Invalid E-Mail, IP or DNS Address detected!\n\nMust either be a valid E-Mail,\nor a valid IP or DNS address - allowing for % subdomain wildcards.");
	document.getElementById("ajaxbg").style.visibility = "hidden";
	return false;

Open in new window

however, on ,line 3 where var dn is defined it shows an error in my editor (adobe dreamweaver) - and unfortunately i am blind to see what exactly the issue is...
could somebody please help?
the references used were http://stackoverflow.com/questions/2063213/regular-expression-for-validating-dns-label-host-name and the ip i just added a basic validation check. the purpose of this is to block certain ip addresses, email addresses and host names from submitting a form (they have been known to spam our system).
oh - google chrome's developer tools, the console reports the following: Uncaught SyntaxError: Invalid regular expression: /^(?![0-9]+$)(?!-)[a-zA-Z0-9%-]{,63}(?<!-)$/: Invalid group but - i do not see how or where as i think that there are multiple groups here - which one is the issue?
Question by:intellisource
  • 3
LVL 12

Accepted Solution

zappafan2k2 earned 2000 total points
ID: 38329471
It's the < character in the last group
try this:
var dn = /^(?![0-9]+$)(?!-)[a-zA-Z0-9%-]{,63}(?!-)$/i;
LVL 10

Expert Comment

ID: 38330386
In addition to the < character, you have two other problems:
1. There is no lower bound for the length of the domain name.
2. Everything you test must simultaneously be a valid email, IP, and DNS.

Try this:
var dn = /^(?![0-9]+$)(?!-)[a-zA-Z0-9%-]{1,63}(?!-)$/i;
if (!ml.test(eml) && !ip.test(eml) && !dn.test(eml)) {

Open in new window


Author Comment

ID: 38331996
unfortunately tdlewis, it is either or not and - it's for a blacklist database. its to block form submission requests - either by email, ip or dns. the first option was correct thanks :)

Author Closing Comment

ID: 38332037
thanks zappafan2k2... wonder why he had that character there? it's not valid in host names...

Author Comment

ID: 38336059
my apologies tdlewis, you were correct regarding the validation if statement... a bit of reverse logic there lol

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

850 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