views:

51

answers:

2

I have a single form input that is for checking domains. Sometimes people type in www. before the domain or .com after the domain name. The service that i use to check availability automatically checks for all top level domains so when people add the .com at the end it becomes redundant. For example the string submitted is domainname.com.com which is clearly invalid.

I understand you can do this on the server side but due to some rather weird circumstance i must use javascript for this. So is regex the solution here ? If so is there some kind of regex generator i can use for this or can someone point me in the right direction with a code snippet perhaps ?

Appreciate any help thanks!

+1  A: 

This page has an example Regex.

function isUrl(s) {
    var regexp = /^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/
    return regexp.test(s);
}

Here is another example.

function isUrl(s) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(s);
}
Jason Rowe
+1  A: 

Well, regex is one possible solution. You can peel off common TLD's like this:

input = input.replace(
    /\.(?:com|net|org|biz|edu|in(?:t|fo)|gov|mil|mobi|museum|[a-z][a-z])$/i, "");

Is that the kind of thing you're looking for?

Alan Moore