Home > Web Front-end > JS Tutorial > body text

Regular implementation in js implements the effect of separating numbers with spaces every four digits

php中世界最好的语言
Release: 2018-03-30 11:23:49
Original
2731 people have browsed it

这次给大家带来js里的正则实现数字每隔四位用空格分隔效果,js里正则实现数字每隔四位用空格分隔效果的注意事项有哪些,下面就是实战案例,一起来看一下。

数字每隔四位用空格分隔代码实例:
分享一段代码实例,它实现了数字每隔四位就用空格分隔。
这样的效果在填写银行卡的时候十分常见,这也是非常人性化的举措。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script>
$(document).ready(function () {
 $('#ant').on('keyup mouseout input', function () {
  var $this = $(this);
  var v = $this.val();
  /\S{5}/.test(v) && $this.val(v.replace(/\s/g, '').replace(/(.{4})/g, "$1 "));
 });
})
</script>
</head>
<body>
<input type="text" id="ant" />
</body>
</html>
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

正则表达式教程的位置匹配教程(附代码)

在JQ中正则验证不能含有中文的方法

The above is the detailed content of Regular implementation in js implements the effect of separating numbers with spaces every four digits. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!