3

Possible Duplicate:
What is the best regular expression to check if a string is a valid URL?

I have this validate function :

function validate_url(url)
    {
        var pattern1= new RegExp(/^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/);
        var pattern2= new RegExp(/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*[.][A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*([.][A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*)*$/);
        if (pattern1.test(url) || pattern2.test(url))
            return true;
        else
            return false;
    }

and I want to check url validation for these patterns :

     * www.example.com
     * http://www.example.com
     * http://example.com
     * http://example.example.com
     * https://www.example.com
     * https://example.com
     * https://example.example.com
     * example.example.com
     * example.com

".com" is optional .

Actually , it was working until I test this url :

      www,google.com 

It accepts the comma !! I tried to remove it from my regular expression and it still accept it ! what's wrong with it ? I made a big search for a suitable regular expression for my case but they were so HUGE and didn't give me what I want . any suggestion ? Thanks in advance

Community
  • 1
  • 1
Sally
  • 87
  • 2
  • 12

0 Answers0