I'm using javascript for auto group numbers for currency when typing in textbox by javascript: it only accept numbers and left, right, home, end, backspace, delete key
function isNumberKey(i)
{
if (i.value.length > 0)
{
var notNumber=new RegExp("[^0-9]","g");
if(i.value.match(notNumber))
{
i.value=i.value.replace(notNumber,""); // Only number ===> OK
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // group for currency ===> NOT OK
}
}
}
AND IN TEXTBOX CALL:
<asp:TextBox ID="Money" onkeyup="isNumberKey(this);" type="tel" runat="server" ></asp:TextBox>
But when Typing numbers it do not group for currency. Pls help me, I must use the javascript function and onkeyup event, no use another solution.
But when Typing numbers it do not group for currency. Pls help me, I must use the javascript function and onkeyup event, no use another solution.
You can try below code:
function isNumberKey(i) {
if (i.value.length > 0) {
var notNumber = /[0-9]/g;
if (i.value.match(notNumber)) {
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
}
}
The result:
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
When you enter 333333, the key cannot be released. The keyup event is sent to an element when the user releases a key on the keyboard.
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
I have already answered in the above post. The keyup event is sent to an element when the user releases a key on the keyboard.
So when you type, you can only keep pressing the keyboard, not release.
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
I tried with two your solutions, but not effect (user can input letter and group like as 3,3,333,3).
I have alot of textbox like money, so i want buld one function and call as onkeyup="isNumberKey(this);".
My function is activities very good, exception left, right, home, end key are no effect.
I want allow user typing only number AND left, right, home, end key.
this is my function:
function isNumberKey(i)
{
if (i.value.length > 0)
{
i.value = i.value.replace(/[^\d]+/g, ''); // Only number
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // group for currency
I tried with two your solutions, but not effect (user can input letter and group like as 3,3,333,3).
This is the test result of my last answer, does this meet your needs?
The ValidChars property in the FilteredTextBoxExtender control will let you set the characters you need to type.
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
yes, that i need, but if apply on my app it does'nt activities as I hope.
I need you help me to edit the my function in order to accept left, right, home. end key
function isNumberKey(i)
{
if (i.value.length > 0)
{
i.value = i.value.replace(/[^\d]+/g, ''); // Only number
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // group for currency
I need you help me to edit the my function in order to accept left, right, home. end key
Do you mean you want to use the left, right, home, end keys in the TextBox?
If so, my code can still work.
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
I see your code is efffect, but I don't know why it can't in my web
Use F12 to check whether there is an error message in the console.
Please show me your complete code.
Best regards,
Sam
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
11 Points
77 Posts
auto group numbers for currency when typing in textbox by javascript
May 08, 2020 06:03 AM|test0101|LINK
dear all
I'm using javascript for auto group numbers for currency when typing in textbox by javascript: it only accept numbers and left, right, home, end, backspace, delete key
But when Typing numbers it do not group for currency. Pls help me, I must use the javascript function and onkeyup event, no use another solution.
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 08, 2020 08:54 AM|samwu|LINK
Hi test0101,
You can try below code:
function isNumberKey(i) { if (i.value.length > 0) { var notNumber = /[0-9]/g; if (i.value.match(notNumber)) { i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); } } }
The result:
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 08, 2020 09:36 AM|test0101|LINK
hi samwu
I tested but it's no good: It can input letter..., and group wrong (if type: 333333 -> 3,3,3,333
image at: https: //pasteboard.co/J7oS7dn.gif
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 08, 2020 09:58 AM|samwu|LINK
Hi test0101,
You can use RegularExpressionValidator control to make textbox only input numbers.
<asp:TextBox ID="Money" onkeyup="isNumberKey(this);" type="tel" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="Money" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>
When you enter 333333, the key cannot be released. The keyup event is sent to an element when the user releases a key on the keyboard.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 08, 2020 10:14 AM|test0101|LINK
hi Samwu
I tested but same as before, still input letter and wrong group currency
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 11, 2020 02:33 AM|samwu|LINK
Hi test0101,
If you want to allowing only certain characters in a TextBox, you can try to use ajax control FilteredTextBoxExtender.
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:TextBox ID="Money" onkeyup="isNumberKey(this);" runat="server"></asp:TextBox> <ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" TargetControlID="Money" ValidChars="1234567890," />
I have already answered in the above post. The keyup event is sent to an element when the user releases a key on the keyboard.
So when you type, you can only keep pressing the keyboard, not release.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 02:22 AM|test0101|LINK
hi Sam
I tried with two your solutions, but not effect (user can input letter and group like as 3,3,333,3).
I have alot of textbox like money, so i want buld one function and call as onkeyup="isNumberKey(this);".
My function is activities very good, exception left, right, home, end key are no effect.
I want allow user typing only number AND left, right, home, end key.
this is my function:
function isNumberKey(i)
{
if (i.value.length > 0)
{
i.value = i.value.replace(/[^\d]+/g, ''); // Only number
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // group for currency
}
}
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 02:53 AM|samwu|LINK
Hi test0101,
This is the test result of my last answer, does this meet your needs?
The ValidChars property in the FilteredTextBoxExtender control will let you set the characters you need to type.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 03:47 AM|test0101|LINK
hi Sam
yes, that i need, but if apply on my app it does'nt activities as I hope.
I need you help me to edit the my function in order to accept left, right, home. end key
function isNumberKey(i)
{
if (i.value.length > 0)
{
i.value = i.value.replace(/[^\d]+/g, ''); // Only number
i.value = i.value.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // group for currency
}
}
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 06:10 AM|samwu|LINK
Hi test0101,
Do you mean you want to use the left, right, home, end keys in the TextBox?
If so, my code can still work.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 07:34 AM|test0101|LINK
Hi Sam,
I see your code is efffect, but I don't know why it can't in my web
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 07:38 AM|samwu|LINK
Hi test0101,
Use F12 to check whether there is an error message in the console.
Please show me your complete code.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 12, 2020 08:18 AM|test0101|LINK
Yes Sam.
This is my code: https://forums.asp.net/post/6301104.aspx
In that code, already:
<asp:ScriptManager id="toolScriptManageer1" runat="server" EnablePartialRendering="false">
</asp:ScriptManager>
so, I only updated your help with:
<asp:TemplateField HeaderText="Salary1" >
<ItemTemplate>
<asp:textbox ID="txtSalary1" onkeyup="isNumberKey(this);" Text='<%# Eval("MySalary1") %>' Width="80px" runat="server"></asp:textbox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary2" >
<ItemTemplate>
<asp:textbox ID="txtSalary2" onkeyup="isNumberKey(this);" Text='<%# Eval("MySalary2") %>' Width="80px" runat="server"></asp:textbox>
<ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender2" runat="server" TargetControlID="txtSalary2" ValidChars="1234567890," />
</ItemTemplate>
</asp:TemplateField>
Contributor
3370 Points
1409 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 13, 2020 09:38 AM|samwu|LINK
Hi test0101,
I read all your code, then wrote a demo based on your code, but it is still work.
This is my tested code:
Have you used F12 to check if there is an error message in the console? and you can also set a breakpoint to debug whether your js method is triggered.
Best regards,
Sam
Member
11 Points
77 Posts
Re: auto group numbers for currency when typing in textbox by javascript
May 13, 2020 09:43 AM|test0101|LINK
Hi Sam
I will review all mycode to detect why it'not apply your help code.