diff --git a/maui/src/OtpInput/SfOtpInput.cs b/maui/src/OtpInput/SfOtpInput.cs index 42337e02..547b3bee 100644 --- a/maui/src/OtpInput/SfOtpInput.cs +++ b/maui/src/OtpInput/SfOtpInput.cs @@ -937,27 +937,11 @@ static void OnValuePropertyChanged(BindableObject bindable, object oldValue, obj string newValueStr = newValue?.ToString() ?? string.Empty; string oldValueStr = oldValue?.ToString() ?? string.Empty; - - if (otpInput.Type == OtpInputType.Number) - { - newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray()); - oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray()); - } - - if (otpInput.ValueChanged != null) - { - if (otpInput.Type == OtpInputType.Number) - { - RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); - } - else - { - RaiseValueChangedEvent(otpInput, oldValue?.ToString() ?? string.Empty, newValue?.ToString() ?? string.Empty); - } - } - - otpInput.UpdateValue(bindable, newValue?? string.Empty); - } + newValueStr = new string(newValueStr.Where(c => !char.IsControl(c)).ToArray()); + oldValueStr = new string(oldValueStr.Where(c => !char.IsControl(c)).ToArray()); + RaiseValueChangedEvent(otpInput, oldValueStr, newValueStr); + otpInput.UpdateValue(bindable, newValue ?? string.Empty); + } } /// @@ -970,7 +954,6 @@ static void OnLengthPropertyChanged(BindableObject bindable, object oldValue, ob { if (bindable is SfOtpInput otpInput && newValue is double newLength && oldValue is double oldLength) { - if (newLength <= 0) { newLength = oldLength; diff --git a/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs b/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs index 0c17db14..da040726 100644 --- a/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs +++ b/maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Editors/SfOtpInputUnitTests.cs @@ -474,6 +474,37 @@ public void RaiseValueChangedEvent_ShouldTrigger_ValueChangedEvent() Assert.True(eventTriggered); } + [Theory] + [InlineData(OtpInputType.Number, "12", "12")] + [InlineData(OtpInputType.Text, "ab", "ab")] + [InlineData(OtpInputType.Password, "xy", "xy")] + public void ValueChangedEvent_WithPartialInput_DoesNotContainNullCharacters(OtpInputType type, string input, string expected) + { + // Arrange + var otpInput = new SfOtpInput + { + Length = 4, + Type = type + }; + string? newValueFromEvent = null; + string? oldValueFromEvent = null; + otpInput.ValueChanged += (sender, e) => + { + newValueFromEvent = e.NewValue; + oldValueFromEvent = e.OldValue; + }; + // Act + otpInput.Value = input; + // Assert + Assert.Equal(expected, newValueFromEvent); + Assert.Equal(string.Empty, oldValueFromEvent); + if (newValueFromEvent != null) + { + Assert.DoesNotContain('\0', newValueFromEvent!); + } + } + + #endregion #region Public Methods