URL regex validation

You have to escape your special characters (/ and that . after www in this case) and att the missing trailing /, like this:

var re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/;
if (!re.test(url)) { 
    alert("url error");
    return false;
}

Leave a Comment