-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?

Community
  • 1
  • 1
iamhimadri
  • 532
  • 1
  • 5
  • 20

1 Answers1

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
num8er
  • 18,604
  • 3
  • 43
  • 57