Shafeeque
Shafeeque

Reputation: 2069

RegEx for URL validation - Javascript ( AngularJs )

I need following URL should be returned as true

Following url should be returned as false

This is regex I tried so far /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$/. Im using this inside angular form input ng-patter

Any help would be appreciated

Upvotes: 0

Views: 1620

Answers (1)

Arzoo
Arzoo

Reputation: 106

Try this regular expression

/^((?:http://)|(?:https://))(www.)?((?:[a-zA-Z0-9]+.[a-z]{3})|(?:\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}(?::\d+)?))([/a-zA-Z0-9.]*)$/gm

function isUrlValid(userInput) {
    var res = userInput.match(/^((?:http:\/\/)|(?:https:\/\/))(www.)?((?:[a-zA-Z0-9]+\.[a-z]{3})|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?::\d+)?))([\/a-zA-Z0-9\.]*)$/gm);
    if(res == null)
        return false;
    else
        return true;
 }

Upvotes: 1

Related Questions