-4

I want to validate URL and i need a function. So user can type HTTP://, HTTPS://, WWW or just facebook.com. I need to make sure that everything works regardless of how they type it.

some think like :

function ValidateUrl(url){

return CorrectUrl;
}

Thank's in advance :)

Muhammad Furqan
  • 324
  • 3
  • 16
  • dont quite get what you want? – Alex Jun 18 '13 at 10:00
  • @Alex if user fill `facebook.com` function should return `https://www.facebook.com/`. – Muhammad Furqan Jun 18 '13 at 10:03
  • 4
    So.... what you're saying is if the user doesn't specify the http/https or www, you still want things that look like URLs to be recognised as URLs? Is that right? So.... what happens if someone is talking about VB.Net? That's a programming language. There's a very good chance that they don't mean it to be a web address. But how is your system going to know that? How can you tell which ones are actually meant to be URLs and which ones are just strings that happen to contain a dot in the middle? – Spudley Jun 18 '13 at 10:04
  • 1
    @Furqan: note that Stack Overflow isn't a service for writing your code for you. Could you clarify what you're asking? – Paul D. Waite Jun 18 '13 at 10:05
  • Also, did you look for identical questions here on SO before posting? There are numerous similar posts already (several of which are listed on the panel to the right). Did you try any of the answers there? Have you got *any* code to show us so we can see what you've done already? – Spudley Jun 18 '13 at 10:07
  • 4
    You can't slap `www.` on the front of arbitrary domain names. `http://www.example.com` and `http://example.com` may be different sites, or one may not exist at all. – Quentin Jun 18 '13 at 10:12
  • @Spudley user will only supply social networking sites url like `https://www.facebook.com/pagename`. if user fill url without HTTPS, or WWW i just need to correct it and save it to database. – Muhammad Furqan Jun 18 '13 at 10:15
  • @Paul D. Waite yes i know. but i am week in regular expressions. – Muhammad Furqan Jun 18 '13 at 10:17
  • 2
    @Furqan: sure, but you don't need to know regular expressions to describe what you're trying to achieve in more detail. At the moment, it sounds like you don't know exactly what you're trying to do. – Paul D. Waite Jun 18 '13 at 10:20
  • okay, so does that mean there's a limited set of sites you're expecting? (if so, you should mention that in the question). And does it mean that the user is entering *just* a URL, or is it part of a bigger string? Is the field they're entering marked as a URL field? The question gives very little context to what you're trying to do. And again, you need to show us a bit more about what you've already tried; you need to explain the problem more clearly. – Spudley Jun 18 '13 at 10:21

2 Answers2

2

Try following function

function validateURL(textval) {
  var urlregex = new RegExp(
        "^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
  return urlregex.test(textval);
}
Pawan
  • 1,065
  • 5
  • 10
  • according to my question `www.facebook.com/pagename` , `http://www.facebook.com/pagename`, both urls are not correct. correct url is `https://www.facebook.com/pagename` so if user enter wrong url function should return correct url by adding https, or www etc .. – Muhammad Furqan Jun 18 '13 at 10:22
-2

As per your requirements:

if user fill facebook.com function should return https://www.facebook.com/

Done:

function ValidateUrl(url){
    var CorrectUrl = 'https://www.facebook.com/';

    return CorrectUrl;
}
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270