-2

There are so many post about url validation using regular expression. I got this Link expression and create a function which validate url. Everything is working fine but i want "some.com" also true, which is i am getting false. I tried with some regular exp for empty string like ^$ but not working fine!

function is_url(url)
    {
        if(/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.)[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/.test(url))
            {
               return true
            }
            else
            {
               return false
            }
        }

Result:

https:\\www.some.com - true

http:\\www.some.com - true

http:\\some.com - true

www.some.com - true

some.com - false // i want this also true

Any Help?

9
  • This isn't an URL at all. At this point, why do a validation ? Commented Jun 5, 2015 at 7:56
  • jsut do www part optional like (http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/|www\.)? Commented Jun 5, 2015 at 7:57
  • 4
    http:\anything is not a valid http url Commented Jun 5, 2015 at 7:57
  • try this: jqueryvalidation.org/url-method Commented Jun 5, 2015 at 8:00
  • @mplungjan..edited now Commented Jun 5, 2015 at 8:03

1 Answer 1

1

try validator.js https://www.npmjs.com/package/validator

isURL(str [, options]) - check if the string is an URL. options is an object which defaults to { protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false }.

bower install validator-js

<script type="text/javascript" src="bower_components/validator-js/validator.min.js"></script>
<script type="text/javascript">
  var urlOptions = {protocols: ['http','https'], require_protocol: false, allow_underscores: false};

  console.log(validator.isURL('https://www.some.com', urlOptions));
  console.log(validator.isURL('http://www.some.com', urlOptions));
  console.log(validator.isURL('http://some.com', urlOptions));
  console.log(validator.isURL('www.some.com', urlOptions));
  console.log(validator.isURL('some.com', urlOptions));
</script> 

here is results:

  1. code: http://joxi.ru/vDr8ooZC3jQGA6
  2. output: http://joxi.ru/gV2VGG4tOMgaAv
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.