Home > Software design >  Regular Expression for username with particular special character
Regular Expression for username with particular special character

Time:09-16

I have to validate username with special character. For example:

raaghu@09$%^&     
@Raaghu098          
Raaghu         
9raghu  

Only these special characters are allowed: %^&@#$^*:'.-_ 

How can I do this?

CodePudding user response:

This should work: ^(?:[a-zA-Z0-9%^&@#$^*:'.\-_] )$

    var pattern = @"^(?:[a-zA-Z0-9%^&@#$^*:'.\-_] )$";
    var validUsername = "raaghu@-09_$%^&";
    var InvalidUsername = "raaghu@09$%^&!";
    
    Regex.IsMatch(validUsername , pattern); // true
    Regex.IsMatch(InvalidUsername , pattern); // false
  • Related