why does the following hostname still not validate?

Posted on 2012-09-03
Last Modified: 2012-09-03
hi, i am trying to add to the blacklist but the following javascript still detects it as invalid - yet it should be allowed...
the value eml contains either an email, ip or host name and is tested against the following perl compatible regular expressions (pcre's).
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 hn = /^(?![0-9]+$)(?!-)[a-zA-Z0-9%-]{1,63}(?!-)$/i;
if (!ml.test(eml) && !ip.test(eml) && !hn.test(eml)) {
	salert("Invalid E-Mail, IP or DNS Address detected!\n\nMust either be a valid E-Mail, or a valid IP or DNS (host name) address - allowing for % subdomain wildcards.");
	document.getElementById("ajaxbg").style.visibility = "hidden";
	return false;

Open in new window

ip addresses validate, as well as email addresses... just hostnames? this is strange, but i must admit - i am no guru with regular expressions!

i thought the previous topic was answered - but apparently not... -_-
Question by:intellisource
    LVL 41

    Expert Comment

    ml.test() fails because the ml RegExp requires a @ sign.
    LVL 74

    Accepted Solution

    The hostname regex does not (currently) permit dots. Given your example string, I would expect that it should. Perhaps try:

    var hn = /^(?![0-9]+$)(?![-.])[a-zA-Z0-9%.-]{1,63}(?![.-])$/i;

    Open in new window


    Author Closing Comment

    thanks for noting that! i believe i missed that, these host names being network names and not internet hostnames... ^^ thanks! :D

    and it was supposed to fail on the email check because it was not an email address but a host name / dns address! ;)

    it checks if all of the conditions fail before kicking out a warning and returning false. so it could either be an email, an ip or a dns address :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    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…

    760 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