1

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!

2
  • What if the user has Javascript disabled? Commented Apr 16, 2010 at 2:13
  • then they get a form that works but they will have to be smart enough to not type .com after their domain name. i am aware of this problem. sometimes dirty problems need dirty solutions :) Commented Apr 16, 2010 at 2:15

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.