键入c#时TextBox中的格式编号 [英] Format number in TextBox when typing in c#

查看:284
本文介绍了键入c#时TextBox中的格式编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一些代码来格式化TextBox中的数字输入,每个三位数用逗号分隔,如下所示:1,648,195但我想在输入时在文本框中格式化输入。我在vb中的工作方式与此类似。 net by selection start property但在c#中我不起作用请帮帮我

I want to write some code to format the numeric inputs in a TextBox that each three digit is seperated with a comma, like this: 1,648,195 But I want to format the input in textbox when typing.I work like it in vb.net by selection start property but in c# I do not work please help me

推荐答案


('input.number') .keyup(功能(事件){



//跳过箭头键

if(event.which> = 37 && event。其中< = 40)返回;



//格式编号
('input.number').keyup(function(event) {

// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;

// format number


(this).val(函数) (指数,价值){

返回值

.replace(/ \D / g,)

.replace(/ \\ \\ B(?=(\d {3})+(?!\ d))/ g,,)

;

});

});



< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.0。 0 / jquery.min.js>< / script>

< input class =number>
(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input class="number">


这篇关于键入c#时TextBox中的格式编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆