Domain Name Validation Using a Regular Expression

advertisements

This question already has an answer here:

  • javascript regular expression validation for domain name? 6 answers

I want to validate domain name (only), which is going to be entered into textbox.

Acceptance Criteria:

abc.com
abc.in
abc.pqr.com
abc.com/xyz
abc.com/xyz/pqr
no.matter.how-many.domain-name.com/anything

Reject:

http://example.com
(anything preceding with http:// or https:// or ftp://)
abc.com////
abc.com///xyz
.....likewise

I have/had tried this:

^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.?)^[a-z0-9]?$/i

But it doesn't take: abc.com/xyz or abc.com/ (that is in my acceptance criteria)

(After that)Also tried this:

/([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/

This overcome above problem,But it also take like: http://example.com (that is in my reject criteria)

I am not more good at Regular expressions, so, what will be the regular expression for above scenario. (Any modifications do i need in existing code?)


This solves my problem:

^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})(\/(?:[\/\w \.-]*)*\/?)?([\/?#].*)?

This Article helps.

Edit: Not Working. Also accepting ..com and example.com/////abc