We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How do I only allow .5 as the valid decimal number inside a Double Text Box?

How do I only allow .5 as the valid decimal number inside a Double Text Box? 

I'll be using this for the user to place a quantity that only accepts whole numbers and a decimal number of .5

Example:
1
1.5
2.5
10
13.5

9 Replies

JP Jagadeesan Pichaimuthu Syncfusion Team June 4, 2019 09:53 AM UTC

Hi Mark 
  
Thank for contacting Syncfusion support. 
 
We have checked your query “How do I only allow .5 as the valid decimal number inside a Double Text Box”. You can achieve your requirement using NumberDecimalDigits property which used to set number decimal digits in DoubleTextBox and ScrollInterval property is used to set the value interval of the DoubleTextBox. We do not have direct support to display the value as like in the provided example. If we set NumberDecimalDigits value at 1, it will display 0 decimal digits for whole numbers. But we have prepared the workaround to achieve your requirement by changing the NumberDecimalDigits based on the DoubleTextBox value in ValuechangedEvent. Please find the code snippet and sample for the same. 
 
Code Snippet: 
<Grid> 
            <syncfusion:DoubleTextBox x:Name="doubleTextBox" Width="100" Height="40"MinValue="1" MaxValue="50" ScrollInterval="0.5" NumberDecimalDigits="1" VerticalAlignment="Center" HorizontalAlignment="Center"/> 
        </Grid> 
  
/// <summary> 
        /// Event triggers when value changed in the DoubleTextBox 
        /// </summary> 
        private void DoubleTextBox_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        { 
            if (doubleTextBox.Value != null) 
            { 
                int result = (int)((doubleTextBox.Value % 1) * 10); 
                if (result == 0) 
                { 
                    doubleTextBox.NumberDecimalDigits = 0; 
                } 
                else 
                { 
                    doubleTextBox.NumberDecimalDigits = 1; 
                } 
            } 
        } 
  
  
Please try this above solution and let us know if it is helpful. 
  
Regards, 
Jagadeesan


MA Mark June 6, 2019 01:34 AM UTC

Thanks but unfortunately there's a couple of problems:

1. I can't type the decimal point, I have to scroll to increase it by .5 (it's not an acceptable behavior according to the users)
2. If there's a point .5 on the number, you can still mess with the decimal number by placing the cursor at the end of the decimal number and typing any number you want.


JP Jagadeesan Pichaimuthu Syncfusion Team June 6, 2019 09:41 AM UTC

Hi Mark 
  
Thanks for your update. 
  
Query 1: 
1. I can't type the decimal point, I have to scroll to increase it by .5 (it's not an acceptable behavior according to the users) 
As stated earlier, we do not have direct support to display the value as like in the provided example. So we have prepared the work around to achieve your requirement. In provided workaround, we have been changing NumberDecimalDigits in ValueChanged event as per your requirement. Due to this behavior, DoubleTextBox restrict the user to type decimal digit when NumberDecimalDigits property value is 0. To avoid this behavior we suggest you to remove the code which we provided in previous update. Please find the code snippet and sample for the same. 
  
Code Snippet: 
/// <summary> 
        /// Event triggers when value changed in the DoubleTextBox 
        /// </summary> 
        private void DoubleTextBox_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        { 
// Remove the code 
            if (doubleTextBox.Value != null) 
            { 
                int result = (int)((doubleTextBox.Value % 1) * 10); 
                if (result == 0) 
                { 
                    doubleTextBox.NumberDecimalDigits = 0; 
                } 
                else 
                { 
                    doubleTextBox.NumberDecimalDigits = 1; 
                } 
            } 
        } 
  
  
Query 2: 
2. If there's a point .5 on the number, you can still mess with the decimal number by placing the cursor at the end of the decimal number and typing any number you want. 
  
Based on provided information, we suspect you want to restrict the cursor placed on the decimal number when there a point .5 on the number. If so, we cannot restrict the cursor placed on the decimal point. 
  
If we misunderstood your requirement, please provide more information regarding the requirement with simple example. It would be more helpful to proceed further. 
  
  
  
  
  
Screenshot: 
  
Please try this above solution and let us know if it is helpful. 
  
Regards, 
Jagadeesan 



MA Mark June 6, 2019 12:00 PM UTC

1. I can't remove the whole number without highlighting it, I can't click in front of the whole number and type in front of it to replace it.
2. The decimal number accepts every number instead of only restricting it to 5.
3. Is there any regex that would handle this instead? It seems the double text box's function is extremely limited.



JP Jagadeesan Pichaimuthu Syncfusion Team June 7, 2019 07:05 AM UTC

Hi Mark 
  
Thanks for your update. 
  
Query 1: 
I can't remove the whole number without highlighting it, I can't click in front of the whole number and type in front of it to replace it. 
  
In DoubleTextBox, by default MaxValidation will occurs when key press on it. Since the Max Value is set as 50 in provided sample so it will not allow the user to type greater than 50. Please find the video for the same. If you want to avoid this behavior you can change MaxValidation property to MaxValidation.OnLostFocus. So that validation will occurs only on lost focus of the DoubleTextBox. Now you can click in front of whole number and replace it by another number. 
 
Code Snippet: 
this.doubleTextBox.MaxValidation = Syncfusion.Windows.Shared.MaxValidation.OnLostFocus; 
  
  
  
Query 2: 
The decimal number accepts every number instead of only restricting it to 5. Is there any regex that would handle this instead? It seems the double text box's function is extremely limited. 
  
Based on provided information, we suspect you want to restrict the decimal number accepts only 5(Eg 3.5). For this you need handle this case in ValueChanged event manually. Please confirm whether your expected behavior is same. Based on your confirmation we will proceed further on this. Currently DoubleTextBox does not have any regex. 
  
  
  
  
  
Please try this above solution and let us know if it is helpful. 
  
Regards, 
Jagadeesan


MA Mark June 7, 2019 09:16 AM UTC

Yes, for the query 2 that's my requirement. Thanks


JP Jagadeesan Pichaimuthu Syncfusion Team June 10, 2019 01:05 PM UTC

Hi Mark 
  
Thanks for your update. 
  
As stated earlier, DoubleTextBox doesn’t have direct support to achieve your requirement “Need to restrict the decimal number accepts only 5(Eg 3.5)”. Currently we are working to achieve your requirement in sample level and we will update more details on 11/06/2019. 
  
Regards, 
Jagadeesan 



MA Mark June 19, 2019 02:23 AM UTC

It's been more than 2 weeks now, you guys seriously haven't come up with a solution yet? I'm really blown away by your support, so far you have consistently let me down on every occasion, this is actually very surreal.


JP Jagadeesan Pichaimuthu Syncfusion Team June 19, 2019 05:17 AM UTC

Hi Mark 
  
Sorry for Inconvenience caused. 
  
As stated earlier, DoubleTextBox doesn’t have direct support to achieve your requirement “Need to restrict the decimal number accepts only 5”. But we have prepared workaround to achieve your requirement. In attached sample we have restricted the decimal digit to allow only 0.5 in the TextChangedEvent of DoubleTextBox. Please find the code snippet and sample for the same. 
  
Code Snippet: 
  this.doubleTextBox.TextChanged += DoubleTextBox_TextChanged; 
        } 
  
        private void DoubleTextBox_TextChanged(object sender, TextChangedEventArgs e) 
        { 
            doubleTextBox.Text = Convert.ToString((double)Math.Truncate((decimal)(doubleTextBox.Value)) + 0.5); 
        } 
  
  
 
Please try this above solution and let us know if it is helpful. 
 
Regards, 
Jagadeesan 


Loader.
Live Chat Icon For mobile
Up arrow icon