Share via

Entry field specific characters

Alexander Blohmé 1 Reputation point
2022-11-17T12:07:50.4+00:00

How can I make it so that users cant enter anything else than 2 letters and 5 digits in this order: AA12345 ? If they for instance try to put a number first it will not register and vice versa

Developer technologies | .NET | Xamarin

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,166 Reputation points Microsoft External Staff
    2022-11-18T02:33:26.057+00:00

    Hello,

    The best way to match a particular string is to match with a regular expression.

    On MAUI, you could refer to the following simple code to match the specific characters.

       private void myEntry_TextChanged(object sender, TextChangedEventArgs e)  
       {  
           Regex regex = new Regex(@"^[a-zA-Z]{2}\d{5}");  
           if (e.NewTextValue != null && e.NewTextValue.Length == 7)  
           {  
               if (!regex.IsMatch(e.NewTextValue))  
               {  
                   test.Text = "Not Match!";  
               }  
               else  
               {  
                   test.Text = "Matched";  
               }  
           }  
           else  
           {  
               test.Text = "Not Match!";  
           }  
       }  
    

    You could refer to this official documentation to get more details: Regex Class.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.