0

I'm trying to create validation for URL which will accept the following cases with HTTP or https or without, with www or without, with subpages or without

http://website.com
https://website.com
http://website.com/
http://www.website.com
website.com
http://website.com/page/id/sdf

I tried the following, but it did not cover all cases above

$scope.urlPattern = '^(([a-z]+[.])?[a-z0-9-]+([.][a-z]{1,4}){1,2}(/.*[?].*)?$'
$scope.urlPattern = '^((https?|ftp)://)?([a-z]+[.])?[a-z0-9-]+([.][a-z]{1,4}){1,2}(/.*[?].*)?$'

I do not have permission to add a comment, So I am editing for my answer only. Below link has all type of URL validation, hope it will help you:

All type URL validation link

2
  • consider the m-flag for multiline search Commented Apr 7, 2017 at 9:40
  • or you can use the ultimate gist.github.com/dperini/729294 Commented Apr 7, 2017 at 9:41

2 Answers 2

1

If you just want to validate the url, you don't really need to concern the 'www' condition (since it is included in other condition)

Something simple can be done like this:

'^(https?:\/\/)*[a-z0-9-]+(\.[a-z0-9-]+)+(\/[a-z0-9-]+)*\/?$'

JSFiddle: https://jsfiddle.net/q0d69jq3/2/

Sign up to request clarification or add additional context in comments.

Comments

0

var result = url.match(/(http(s)?://.)?(www.)?[-a-zA-Z0-9@:%.+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%+.~#?&//=]*)/g);

    if (result == null) {
                            return false;
                        }
                        return true;
                    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.