locked
Regex to validate URL RRS feed

  • Question

  • User1192570106 posted

    Hi All,

    I need help to validate URL of below pattern.

    but not allowed url like www.google

    I am using (http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)? this regx but its validating above url.

    Thanks in advance

    Tuesday, September 6, 2016 10:53 AM

Answers

  • User283571144 posted

    Hi Pratap09,

    but not allowed url like www.google

    I am using (http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)? this regx but its validating above url.

    Thanks in advance

    According to your description, I suggest you could try below codes:

     <script>
            $(function () {
                var expression = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[-a-zA-Z0-9@:%._\+~#=]{2,256}/g;
                var regex = new RegExp(expression);
                var t = "www.google";
                var t1 = "www.google.com";
                var t2 = "http://www.google.com";
                var t3 = "https://www.google.anydomain";
    
                if (t.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t1.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t2.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t3.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
            });
        </script>

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2016 1:24 PM

All replies

  • User283571144 posted

    Hi Pratap09,

    but not allowed url like www.google

    I am using (http:\/\/|https:\/\/)?(www.)?([a-zA-Z0-9]+).[a-zA-Z0-9]*.[a-z]{3}.?([a-z]+)? this regx but its validating above url.

    Thanks in advance

    According to your description, I suggest you could try below codes:

     <script>
            $(function () {
                var expression = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[-a-zA-Z0-9@:%._\+~#=]{2,256}/g;
                var regex = new RegExp(expression);
                var t = "www.google";
                var t1 = "www.google.com";
                var t2 = "http://www.google.com";
                var t3 = "https://www.google.anydomain";
    
                if (t.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t1.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t2.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
                if (t3.match(regex)) {
                    alert("Successful match");
                } else {
                    alert("No match");
                }
            });
        </script>

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 6, 2016 1:24 PM
  • User445449386 posted

    Regex if you want to ensure URL starts with HTTP/HTTPS:

    https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

    If you do not require HTTP protocol:

    [-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

    Javascript Code

    <script>
    var expression = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
    var regex = new RegExp(expression);
    var t = 'www.google';
    
    if (t.match(regex)) {
      alert("Successful match");
    } else {
      alert("No match");
    }
    </script>

    Wednesday, September 7, 2016 4:34 AM
  • User1192570106 posted

    Thanks a ton Brando ZWZ...

    Wednesday, September 7, 2016 7:06 AM