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