0

I would like to check if the given url is valid.

It should accept:

www.gmail.com

and should reject:

www.gmail

I tried using this /((ftp|http|https):\/\/)?(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

It works but if the given input is www.gmail it does accepts it.

Any Ideas?

UPDATE

http://jsfiddle.net/aabanaag/VktaX/

n0minal
  • 3,195
  • 9
  • 46
  • 71
  • possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Toto Mar 20 '13 at 10:00
  • This seems to me like a duplicate of this question: http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url Did you check that out? –  Mar 20 '13 at 07:40

3 Answers3

0

try this .It is working for me

<script type="text/javascript">
    function validate() {
        var url = document.getElementById("url").value;
        var pattern = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
        if (pattern.test(url)) {
            alert("Url is valid");
            return true;
        } 
            alert("Url is not valid!");
            return false;

    }
</script>

In jQuery

you can use url validation

<input type="text" class="url"/>
PSR
  • 39,804
  • 41
  • 111
  • 151
0

You could use Jquery Validate plugin for that.

$("#myform").validate({
  rules: {
    field: {
      required: true,
      url: true
    }
  }
});

You can find about it here

Yash
  • 177
  • 2
  • 14
0

solved my problem:

found this while googling over the internet.

 /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$/


var pattern1 = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$/;

var result = "www.gmail";

if (pattern1.test(result)) {
    console.log("test");
}else{
    console.log("fail");
}

It works best.

n0minal
  • 3,195
  • 9
  • 46
  • 71