Forum Xamarin.iOS

Entering Date in dd/MM/yyyy format using numeric keyboard in UITextField xamarin.ios

AmejackAmejack USMember ✭✭✭
edited April 14 in Xamarin.iOS

i m finding this in swift .but i m unable to convert it in xamarin.ios

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //Format Date of Birth dd/MM/yyyy

    //initially identify your textfield

    if textField == txtDOB {

        // check the chars length dd -->2 at the same time calculate the dd-MM --> 5
        if (txtDOB?.text?.characters.count == 2) || (txtDOB?.text?.characters.count == 5) {
            //Handle backspace being pressed
            if !(string == "") {
                // append the text
                txtDOB?.text = (txtDOB?.text)! + "/"
            }
        }
        // check the condition not exceed 9 chars
        return !(textField.text!.characters.count > 9 && (string.characters.count ) > range.length)
    }
    else {
        return true
    }
}
Tagged:

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
     txtDOB.ShouldChangeCharacters += (UITextField textField, NSRange range, string replacementString) => {
    
                if(textField == txtDOB)
                {
                    if (txtDOB.Text.Length == 2 || txtDOB.Text.Length == 5)
                    {
                        if(!(replacementString == ""))
                        {
                            txtDOB.Text = txtDOB.Text + "/";
                            return !(textField.Text.Length > 9 && replacementString.Length > range.Length);
                        }
                    }
                }
                return true;
            };
    
Sign In or Register to comment.