-1

I would like to validate url's in javascript before user proceeds further.

The urls below should match;

  • http://google.com
  • http://www.google.com
  • www.google.com
  • google.com

And not match;

  • http://google
  • http://www.google
  • www.google
  • google

Please help me im really bad at regex

Rouvé
  • 415
  • 5
  • 4

1 Answers1

0

try like this

     var urlR = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)
       (?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
     var url= content.match(urlR);

or

  var regex = new RegExp("^(http[s]?:\\/\\/(www\\.)?|ftp:\\/\\/(www\\.)?|www\\.){1}([0-9A-Za-z-\\.@:%_\+~#=]+)+((\\.[a-zA-Z]{2,3})+)(/(.)*)?(\\?(.)*)?");

     if(regex.test("http://google.com")){
      alert("Successful match");
    }else{
    alert("No match");
      }
Nikhil Ghuse
  • 1,258
  • 14
  • 31