如何验证货币输入 [英] How to validate a currency input

查看:90
本文介绍了如何验证货币输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表单,其中包括工资,我需要检查工资的输入值是否是有效货币并且有两个小数位,因为这是数据库接受的。如果薪水输入有字母(az)或符号(!@#%^& *()除了$或其他货币符号),它应该改变边框颜色。

I am creating a form, and that includes salary, I need to check if the input value for salary is a valid currency and has two decimal places because that is what the database accepts. If the input for salary has alphabets (a-z) or symbols (!@#%^&*() except for the $ or other currency sign) it should change the border color.

示例:

  10000.00
  25846.00
  213464.12

代码:

function isNumeric(){
   var numeric = document.getElementById("txtSalary").value;
   var regex  = /^\d+(?:\.\d{0,2})$/;
   if (regex.test(numeric)){
     $("#txtSalary").css("border-color","#FFFFFF");
   }else{
      $("#txtSalary").css("border-color","#FF0000");
   }
 }  

这已经按照我的意愿运作,但是问题是我还有6个输入框需要这种验证。如何在调用时更改特定选择器的border-color并返回false,该值是否为数字,如下所示:

This is already working as I wanted too, but the problem is I got 6 more input boxes that needs this kind of validation. How can I make that function, when called change the border-color of the specific selector and will return false is the value is not numeric something like:

isNumeric(selector);

function isNumeric(selector){
    var regex  = /^\d+(?:\.\d{0,2})$/;
     $(selector).filter(function() {
        return (regex.test(this.value));
     }).css("border-color","#FF0000");
    }

谢谢!

推荐答案

尝试直接在输入中使用html5模式:

try to use html5 pattern directly on your input:

<input type='text' pattern='^\d+(?:\.\d{0,2})$'/>

它适用于所有现代浏览器。
如果模式为假,它将使边框变为红色

it works with all modern browsers. it will make the border red if the pattern is false

这篇关于如何验证货币输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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