JS URL Validation

I tried to check the url with or without http. No matter what I did, the function returns false. I checked the regex line on this site: http://regexr.com/ And it can be seen, as I expect.

    function isUrlValid(userInput) {
        var regexQuery = "/(http(s)?://.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/";
        var url = new RegExp(regexQuery,"g");
        if (url.test(userInput)) {
            alert('Great, you entered an E-Mail-address');
            return true;
        }
        return false;
    }

I fix the problem by changing the .test value to .match and leaving the regex as it is.

+9
source share
4 answers

I change the function to match + make changes here with a slash and its operation: (http (s)?: //.)

Fixed function:

function isUrlValid(userInput) {
    var res = userInput.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
    if(res == null)
        return false;
    else
        return true;
}
+21
source

, url (, ) (, http://www.-example-.com www.% @&.com). URL ( ).

function isUrlValid(userInput) {
    var regexQuery = "^(https?://)?(www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-z0-9]{0,61}[a-z0-9]\\.[a-z]{2,6}(/[-\\w@\\+\\.~#\\?&/=%]*)?$";
    var url = new RegExp(regexQuery,"i");
    return url.test(userInput);
}
var input = ["https://o.sub-domain.example.com/foo/bar?foo=bar&boo=far#a%20b",
             "HTTP://EX-AMPLE.COM",
             "example.c",
             "example-.com"];
for (var i in input) document.write(isUrlValid(input[i]) + ": " + input[i] + "<br>");

IP- , :

"^(https?://)?(((www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-zβ€Œβ€‹0-9]{0,61}[a-z0-9]\\β€Œβ€‹.[a-z]{2,6})|((\\d{1β€Œβ€‹,3}\\.){3}\\d{1,3}))β€Œβ€‹(:\\d{2,4})?(/[-\\w@β€Œβ€‹\\+\\.~#\\?&/=%]*)?$β€Œβ€‹"  

( , ), :

"^(https?://)?(((www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-zβ€Œβ€‹0-9]{0,61}[a-z0-9]\\β€Œβ€‹.[aβ€Œβ€‹-z]{2,6})|((\\dβ€Œβ€‹{1,3}\\.){3}\\d{1,3}β€Œβ€‹))(:\\d{2,4})?((/|\\β€Œβ€‹?)[-\\w@\\+\\.~#\\?&β€Œβ€‹/=%]*)?$"

, % , :

"^(https?://)?(((www\\.)?([-a-z0-9]{1,63}\\.)*?[a-z0-9][-a-zβ€Œβ€‹0-9]{0,61}[a-z0-9]\\β€Œβ€‹.[a-z]{2,6})|((\\d{1β€Œβ€‹,3}\\.){3}\\d{1,3}))β€Œβ€‹(:\\d{2,4})?((/|\\?)β€Œβ€‹(((%[0-9a-f]{2})|[-\β€Œβ€‹\w@\\+\\.~#\\?&/=])*β€Œβ€‹))?$"

(: , ).

+4

.

function CheckURL(fieldId, alertMessage) {
    var url = fieldId.value;
    if(url !== "")
    {
        if (url.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g) !== null)
            return true;
        else {
            alert(alertMessage);
            fieldId.focus();
            return false;
        }
    }
}

var website = document.getElementById('Website');
if (!CheckURL(website, "Enter a valid website address")) {
    return false;
}
0

In case someone needs a regexp with finding URLs like these:

  1. https://www.youtube.com/watch?v=38XmKNcgjSU
  2. https://www.youtube.com/
  3. www.youtube.com
  4. youtube.com ...

I came up with this regex:

((http(s)?://)?([\w-]+\.)+[\w-]+[.com]+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)
0
source

All Articles