[CLOSED] Textfield with Decimal- and Thousandseparator

  1. #1

    [CLOSED] Textfield with Decimal- and Thousandseparator

    Good evening all

    I need to display numbers in a Textfield. the numbers should be displayed with a '.' as decimal separator and a "'" as thousand separator.
    The user should be able to enter any kind of decimal values and the control should display the values in a formatted style on blue. When the control gets back the focus, the formatting should be removed.

    Values like 123456,78 -> 123'456.78
    or 1234,567 -> 1'234.567

    Is there a way to do this ?

    Peter
    Last edited by fabricio.murta; May 03, 2016 at 6:33 PM.
  2. #2
    Hello Peter!

    I believe what you want is covered on this thread: Problem with DecimalSeparator.

    It shows what to do onBlur to format the number. What's left is just to make the onFocus to clean up the mask.

    Hope this helps!
    Fabrício Murta
    Developer & Support Expert
  3. #3
    Thanks for the Example Fabricio
    I tried to implement it on a similar way but I need to remove the thousand separator as soon as the user enters the field.

    My example seems to work, the formatting is applied as soon as the cursor leafs the field. The only 'problem' my Regex is based on the formatted Value. This means when I enter the value, the Regex alerts...

    Is there a way to stop the Regex check while entering the data and check it later, as soon as the Formatting is applied ?

    And what can I do to call the Formatter when the Value is added via Code -> Click the Button in the Example...


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Formatter.aspx.cs" Inherits="Ext.Net.Forum.Formatter" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head runat="server">
      <title></title>
    </head>
    <script type="text/javascript">
      function formatNumber(amount, decimals) {
    
        var pattern;
        switch (decimals) {
          case 1:
            pattern = "0,000.0";
            break;
          case 2:
            pattern = "0,000.00";
            break;
          case 3:
            pattern = "0,000.000";
            break;
     
          default: pattern = "0,000.00";
        };
    
        // Save the separator
        var thousandSep = Ext.util.Format.thousandSeparator;
        Ext.util.Format.thousandSeparator = "'";
        var decimalSep = Ext.util.Format.decimalSeparator;
        Ext.util.Format.decimalSeparator = '.';
    
        var formatted = Ext.util.Format.number(amount, pattern);
    
        // restore  separator
        Ext.util.Format.thousandSeparator = thousandSep;
        Ext.util.Format.decimalSeparator = decimalSep;
    
        return formatted;
      };
    
      function unformatNumber(amount) {
        return amount.replace(/'/g, '');
      };
    
    </script>
    
    <body>
      <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManagement" runat="server">
          <Scripts>
          </Scripts>
        </asp:ScriptManager>
        <ext:ResourceManager ID="ResourceManager1" runat="server" ScriptMode="Release" DisableViewState="true" StateProvider="Cookie">
          <Listeners>
          </Listeners>
        </ext:ResourceManager> 
    
        <ext:Container runat="server" Layout="Form">
          <Items>
            <ext:Label runat="server" Text="Enter something like : '123456.987' " PaddingSpec="80 0 0 50"></ext:Label>
            <ext:Label runat="server" Text="RegEx for this Field : ^(\d{0,3}(\'\d{3}){0,2})(\.\d{0,3})?$" PaddingSpec="80 0 0 50"></ext:Label>
            <ext:TextField runat="server" ID="txtTest" FieldLabel="Input" Width="300" RegexText="Decimal (9.3)" Regex="^(\d{0,3}(\'\d{3}){0,2})(\.\d{0,3})?$" PaddingSpec="100 0 0 50">
              <Listeners>
                <Blur Handler="this.setValue(formatNumber(this.getValue(),3));" />
                <Focus Handler="this.setRawValue(unformatNumber(this.getValue()));" />
              </Listeners>
            </ext:TextField>
            <ext:Button runat="server" Text="Set a Value" MarginSpec="0 0 0 50">
              <Listeners>
                <Click Handler="#{txtTest}.setValue('123456.123');"></Click>
              </Listeners>
            </ext:Button>
          </Items>
        </ext:Container>
      </form>
    </body>
    </html>
    Kind Regards
    Peter
    Last edited by Peter.Treier; Apr 28, 2016 at 1:43 PM.
  4. #4
    OK, I found a way to set the Value

    <Click Handler="#{txtTest}.setValue(formatNumber('123456.123',3));"></Click>
  5. #5
    Hello Peter!

    Glad you could find the solution! And thanks for sharing it here! I'm sure this will come handy in the future for people with similar needs!

    EDIT: seems you still need to figure out the Regex issue, right?
  6. #6
    Okay, about the regexp, why not leave the single quote "optional" in the string? So both ways are valid -- but your form maintains the nice formatting you need?

    You know, formal value validation should always be done server side, a skilled user can always trick the browser into letting go an invalid value. And with this validation, as far as it can surround it, the user will only be sending a valid number to the server (when the form is updated, for example).

    Additionally, your regexp allowed this: '123.123 and .123 I suppose that's not what you want (if not, just ignore the first match portion of the regexp below):
    Regex="^(\d{1,3}((|\')\d{3}){0,2})(\.\d{0,3})?$"
    Is it an acceptable approach?
    Fabrício Murta
    Developer & Support Expert
  7. #7
    Hi Fabricio
    We changed our Regex to check the new Field content, thank you :-)

    Peter

Similar Threads

  1. [CLOSED] Decimal in NumericAxis
    By xeo4.it in forum 3.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 14, 2016, 4:40 PM
  2. Replies: 6
    Last Post: Nov 30, 2012, 4:52 PM
  3. How to format a number with 3 decimal
    By NishaLijo in forum 1.x Help
    Replies: 1
    Last Post: Nov 24, 2010, 5:44 AM
  4. [CLOSED] NumberField: decimal precision
    By RomualdAwessou in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Aug 14, 2010, 12:05 AM
  5. Decimal in NumberField
    By olimpia in forum 1.x Help
    Replies: 2
    Last Post: Aug 21, 2009, 10:18 AM

Posting Permissions