How to type the date in entry field xamarin forms

Surendhar Kabilan 1 Reputation point
2023-05-04T08:06:07.3233333+00:00

I have an requirement. When the user tap on the calendar image only at that date picker will open - working fine. If user click entry means number keyboard will open and they will be able to type the date mm/dd/yyyy(validation applied) in that entry field.[enter image description here

](https://i.stack.imgur.com/ReASr.jpg).I have attached the image for your reference.

How to achieve this functionality for both android and ios using xamarin forms?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2023-05-08T07:26:14.7566667+00:00

    Hello,

    You can create a custom Entry, detect the input characters, if the input format is correct and convert it to the MM/dd/yyyy format.

    public class MyEntry : Entry
        {
            public MyEntry(){
    
               
            }
            protected override void OnTextChanged(string oldValue, string newValue)
            {
                base.OnTextChanged(oldValue, newValue);
                DateTime date;
                if (newValue != null)
                {
                    if (DateTime.TryParseExact(newValue, "MMddyyyy", null, System.Globalization.DateTimeStyles.None, out date))
                    {
                        this.Text = date.ToString("MM/dd/yyyy");
                        //Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("The number entered is not a valid date format!");
                    }                                              
            }
        }
    

    You can use it in the layout like following code.

     <local:MyEntry Keyboard="Numeric"></local:MyEntry>
    

    Best Regards,

    Leon Lu


    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.

    
    
    0 comments No comments