javascript email, ip or dns validation

Posted on 2012-08-24
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 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
    LVL 12

    Accepted Solution

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

    Expert Comment

    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

    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

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

    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    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…

    759 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