Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

morloderex's avatar

Change validation rule based on the input

Hello guys :)

I may have a question that is under the skill level of your all but i just can't get my head around this: Let's say i have a login form with the fallowing:

1. A text field where you can use both your email and your username
2. A password field obviously

How you i go about validating that on the server site based of if the input is an email or the username?

Should i use a regex pattern for this to determine if the value is an email and that put in the validator rule or what will you do in this situation?

0 likes
2 replies
DarkRoast's avatar

I think that would be fine especially if you have all your authentication stuff in one place like a service class or command handler. I would however use the filter_var function instead of a regex:

if (filter_var($userId, FILTER_VALIDATE_EMAIL))
{
        // auth with email
}
else
{
       // auth with some other id
}
morloderex's avatar

@WookieMonster I was thinking of extending the validator class with something like that but i would perfer if there was some allready buildin functionality for determine if one rule should be used over another :)

Please or to participate in this conversation.