开发者

Valid domain name [duplicate]

开发者 https://www.devze.com 2023-02-01 06:54 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: PHP validation/regex for URL
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

PHP validation/regex for URL

I'm building a small app in PHP, and have to validate domain names (for use in E-mail addresses). For instance if I have mads@gmail.com I want to check if gmail.com is a valid domain name (I'm essentially throwing away the part before the @).

I have been using the following code:

if (!preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", trim($valid_emails[$i]))) {
         // Return error
}

And everything has been working fine. Until I got GE MoneyBank as a client. They are using ge.com as their domain, and开发者_运维知识库 that doesn't pass.

I have a rudimentary understanding of regex, and have tried deleting some of the [a-z0-9] blocks so the minimum character count comes down to 2, but without luck. Can anyone with more experience point me in the right direction?

Thanks in advance


What you currently have does the following:
- [a-z0-9]: A letter or a number
- [a-z0-9\-]+: A letter or a number or a hyphen, 1 or more times
- [a-z0-9]: A letter or a number

Your problem is that it is expecting at least 3 letters.

You need to make sure it can accept only two letters, and the last one can't be a hyphen.

So you need to change the middle expression to 0 or more times:
- [a-z0-9\-]*: A letter or a number or a hyphen, 0 or more times

0

精彩评论

暂无评论...
验证码 换一张
取 消