织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

How to mask phone number as 123

时间:2025-09-04 15:16来源: 作者:admin 点击: 5 次
Here is a demo to show how to bind data of Model with format 111-111-1111,and when change the input with the format 111-111-1111. Controller: public I

Here is a demo to show how to bind data of Model with format 111-111-1111,and when change the input with the format 111-111-1111. Controller:

public IActionResult Index() { TestReadOnly t = new TestReadOnly { PhoneNumber = "1231231234" }; return View(t); }

View:

@model TestReadOnly @{ ViewData["Title"] = "Index"; } <h1>Index</h1> <input asp-for="PhoneNumber" type="tel" aria-label="Please enter your phone number" placeholder="ex. 111-111-1111" onchange="change()"> @section scripts{ <script type="text/javascript"> $(function () { //bind the data from Model with format 111-111-1111 let txt = JSON.stringify(@Model.PhoneNumber); if (String(txt)[3] != "-") { txt = '' + txt.substring(0, 3) + "-" + txt.substring(3, 6) + "-" + txt.substring(6); } $('[type="tel"]').val(txt); }) //when change the input value,format 111-111-1111 function phoneMask() { var num = $(this).val().replace(/\D/g, ''); $(this).val(num.substring(0, 3) +'-'+ num.substring(3, 6) + '-' + num.substring(6, 10)); } $('[type="tel"]').keyup(phoneMask); </script> }

result:

enter image description here

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-06 03:09 最后登录:2025-09-06 03:09
栏目列表
推荐内容