如何在ASP.NET的TextBox输入中添加连字符? [英] How to add a hyphen in a TextBox input in ASP.NET?

查看:77
本文介绍了如何在ASP.NET的TextBox输入中添加连字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET文本框,如果输入的长度> 5,我想在文本框输入中添加连字符.例如,如果键入123456789,则该条目应显示为:12345-6789

I have an ASP.NET textbox where I want to add a hyphen in the textbox input if the length of the input is > 5. For example, if I type 123456789, the entry should show like this: 12345-6789

该怎么做?

<asp:Button ID="Button1" runat="server" Text="Button" />

推荐答案

$('.phone').on('input', function() {
  this.value.length < 5 || this.value.charAt(5) == '-' || 
    $(this).val( [this.value.slice(0,5), '-', this.value.slice(5)].join('') );
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="phone" class="phone"/>

这篇关于如何在ASP.NET的TextBox输入中添加连字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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