Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to create a custom validator for my field UsernameOrEmail, i.e. I need to provide different validations depending on the contents of the field. Is it a username or an email?
How to specify the condition in the code to distinguish?
Can somebody guide me with the syntax of .when() and .otherwise() inside a IRuleBilder method?

What I have tried:

Here is some (pseudo)code:

C#
public static IRuleBuilder UsernameOrEmail(this IRuleBuilder ruleBuilder)
{
   return ruleBuilder
     .NotEmpty()

     .When( ???condition???, () => //in case UsernameOrEmail is an eMail – check if string is not null and string contains the char ‘@’
     // ??? no clue here about the required syntax. Somthing like this: em => em != null && em.Contains(‘@’), () => ???
     {
        .Email() //perform validation for an email address
     }
     ).Otherwise( () => //it is a username
     {
        .Username() //perform validation for a username
     });
}
Posted
Comments
[no name] 10-Mar-21 13:29pm    
That's your first problem: user name OR email; a field with multiple personalities. If someone wants to use (some) email as a "user name", then it's a name, NOT an email; the "real" email goes in a separate field.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900