0

Possible Duplicate:
how can i validate a url in javascript using regular expression

I want to validate whether user input a valid internet adress like www.hotmail.com http://www.abc.com.pk

3
  • Define "valid." Does the address have to be FORMATTED correctly (so that www.example is invalid, but www.example.co.uk is valid)? Or does it have to actually BE valid (so that www.this-domain-is-not-actually-registered.com is invalid)? If it has to BE valid, for what purpose? Must there be a website there? Or is it okay if the domain has valid and working MX records but no website? Commented Jun 19, 2010 at 13:47
  • Possible duplicate: stackoverflow.com/questions/1420896/… Commented Jun 19, 2010 at 13:50
  • 1
    And define "Internet address". Do you mean an "http URI"? FQDN? (You give examples both of these.) An email address is ad much an "internet address" as either of those examples (as are many other things). Commented Jun 19, 2010 at 13:54

1 Answer 1

0

Here's the js function:

function validateString(regexPattern, testString) {
 var regexObj = new RegExp(regexPattern);
 return regexObj.test(testString);
}

Finding the regex for URLs is just a matter of typing it in google. Here's a good one I've used before.

Sign up to request clarification or add additional context in comments.

2 Comments

Omg, why not just do if (/[somepattern]/i.test(url)) {...}?
cause I like if(validatestring("regex","test")){} better! :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.