快速jQuery数字输入格式化程序和验证程序

  • 源码大小:9.25KB
  • 所需积分:1积分
  • 源码编号:19JP-3078
  • 浏览次数:905次
  • 最后更新:2023年05月03日
  • 所属栏目:表单
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

一个简单快速的jQuery数字输入格式化程序插件,用于格式化和验证输入字段中键入的数值。

它的工作原理是获取输入字段的值,并在同级DOM元素中呈现数字的格式化版本。这使得用户更容易识别和验证他们输入的号码。此外,如果输入不是有效的数字,插件会自动检测输入类型并显示错误消息。

如何使用它:

1.在页面上包含Number Input Formatter插件的JS文件。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  2. <script src="/path/to/jquery.number-input-formatter.js"></script>

2.只需调用输入字段上的函数,插件就可以完成剩下的工作。

  1. <input class="example" value="12345.678">
  2. <span></span>
  1. $(function(){
  2. $('.example').numberInputFormatter();
  3. });
  1. // Output
  2. => 12,345.678

3.在指定的元素中显示格式化的数字。

  1. $(function(){
  2. $('.example').numberInputFormatter({
  3. confirmationSelector: '.another-element',
  4. });
  5. });

4.当输入无效时,显示一条错误消息。

  1. $(function(){
  2. $('.example').numberInputFormatter({
  3. errorSelector: '.error',
  4. });
  5. });

5.自定义错误消息。

  1. $(function(){
  2. $('.example').numberInputFormatter({
  3. errorSelector: '.error',
  4. errorMessage: 'That\'s not a number!',
  5. });
  6. });

预览截图