-2

I have an input box where user can enter their domain. I want to make sure the domain should be like that:

example.com
hello.com
okay.com

NOT

www.example.com
https://www.hello.com
https://okay.com

I mean no http, https and www at the beginning of the domain.

So far I got this:

} elseif( !preg_match('/^[a-zA-Z. ]+$/', $domain) ) {
    $output['message'][] = 'Your domain should be start with http, https or www but can contain (.)com';
} 

How can I improve this preg_match function to get the desire validation?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Shibbir
  • 1,963
  • 2
  • 25
  • 48
  • Does this answer your question? [Using parse\_url in php, how do we check whether input is a url or just a string](https://stackoverflow.com/questions/8046587/using-parse-url-in-php-how-do-we-check-whether-input-is-a-url-or-just-a-string) – DarkBee Jun 09 '22 at 08:01
  • `^\w+.com$` https://regex101.com/r/ydoYEq/1 – shingo Jun 09 '22 at 08:01
  • @shingo do you mean this `!preg_match('^\w+.\w+$', $domain)` – Shibbir Jun 09 '22 at 08:02
  • 1
    `and www at the beginning of the domain` -- please clarify why subdomains are not permitted? – AD7six Jun 09 '22 at 08:02
  • 1
    _"Your domain should be contain only characters."_ - that is quite a misleading error message, when your pattern actually allows not only for dots, but for _spaces_ for some reason as well. And do you really want to reject any user input here, if their domain name would contain digits or hyphens ...? – CBroe Jun 09 '22 at 08:03
  • I will change the message @CBroe but I need only domain name without the http, https and www at the beginning. – Shibbir Jun 09 '22 at 08:04
  • This regex will be incorrect because it can also pass `......`. Better to use parse_url() and filter url functions. – nice_dev Jun 09 '22 at 08:06
  • 1
    @shingo That regex fails for `s3.amazonaws.com` – nice_dev Jun 09 '22 at 08:07
  • 1
    Does this answer your question? [PHP validation/regex for URL](https://stackoverflow.com/questions/206059/php-validation-regex-for-url) - if you just want the domain without a protocol, why not strip it from user input before using it – AD7six Jun 09 '22 at 08:08
  • @AD7six no, I just want to modify my current `preg_match` function so that user can't input http, https or www – Shibbir Jun 09 '22 at 08:09
  • 1
    Why are you insisting on using a regex when there are specific functions for validating URL's? – DarkBee Jun 09 '22 at 08:10
  • @Shibbir You can't be stern with your approach. – nice_dev Jun 09 '22 at 08:11
  • @AD7six which question I should tell you (why)? – Shibbir Jun 09 '22 at 08:11
  • I think you don't understand what I Need. – Shibbir Jun 09 '22 at 08:12
  • 1
    @Shibbir Is nicedev's example `s3.amazonaws.com` also to be considered? – shingo Jun 09 '22 at 08:14
  • My application needs this validation. So if anyone can help me that would be better. – Shibbir Jun 09 '22 at 08:14
  • @shingo Yes, it will be accepted. – Shibbir Jun 09 '22 at 08:14
  • And how about `example.123`? Is that a valid input? – shingo Jun 09 '22 at 08:14
  • @shingo Yes, I just don't want the http, https or www from the begging of the string. – Shibbir Jun 09 '22 at 08:15
  • So `http.example.com` is not valid too? I mean no `://` but `http` is at the beginning. – shingo Jun 09 '22 at 08:17
  • But `123` is not [a valid top-level domain](https://stackoverflow.com/questions/9071279/number-in-the-top-level-domain)... – DarkBee Jun 09 '22 at 08:19
  • @shingo only `example.com` will be valid. – Shibbir Jun 09 '22 at 08:20
  • 2
    _"I think you don't understand what I Need"_ - and I am not too sure _you_ understand what you need :-) _"I just don't want the http, https or www from the begging of the string"_ - that alone would not be a reason to reject domain names that contain digits or hyphens then. – CBroe Jun 09 '22 at 08:31

1 Answers1

0

Try this /^(?!(www|http|https)\.)\w+(\.\w+)+$/

It matches

example.com
hello.com
okay.com
example.123
1.example.com.cn
s3.amazonaws.com

It doesn't match

www.example.com
https://www.hello.com
https://okay.com
http.example.com

shingo
  • 18,436
  • 5
  • 23
  • 42
  • Thanks for your reply but what about `httpyahoo.com` or `httpsyahoo.com` or `wwwyahoo.com` ? – Shibbir Jun 09 '22 at 08:30
  • They match, if you don't want to match these domains, just remove the first dot. – shingo Jun 09 '22 at 08:31
  • I did it but not working. I enter this text `httpyahoo.com` but showing me my validation error. Its should be passed. – Shibbir Jun 09 '22 at 08:33
  • I am using this `! preg_match('/^(?!(www|http|https))\w+(.\w+)+$/', $domain)` – Shibbir Jun 09 '22 at 08:33
  • Sorry, I missed backslashes before dot. – shingo Jun 09 '22 at 08:34
  • I fear this will haunt you in the future @shingo as an example of the problem with providing answers which exactly match the request but are a poor (or entirely inappropriate) solution for the job :). That regex arbitrarily prevents some [valid](https://www.namecheap.com/domains/registration/results/?domain=https) [domains](https://www.namecheap.com/domains/registration/results/?domain=www) and subdomains whist allowing junk like `not.a.domain` to pass. – AD7six Jun 09 '22 at 13:14
  • @AD7six but op had confirmed in the comments above that `https.example.com` is invalid (valid domain) and `example.123` is valid (not a domain) – shingo Jun 09 '22 at 14:00