jQuery Regex仅允许使用货币符号和数字 [英] jQuery Regex to allow only a currency symbol and numbers

查看:93
本文介绍了jQuery Regex仅允许使用货币符号和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery的验证插件来验证表单文本字段. 我试图通过仅允许使用货币符号,字母和数字而不允许其他特殊字符来验证价格字段.

I'm using jQuery's validation plugin to validate form text fields. I'm trying to validate a price field by only allowing currency symbols, letters and numbers but no other special characters.

我已经尝试过了,但是它允许使用其他特殊字符,例如; ":

I have tried this but it's allowing other special characters such as ; " :

^[a-zA-Z 0-9 € £]+$

我做错了什么? 谢谢!

What am I doing wrong? Thanks!

document.ready函数中的代码:

Code in document.ready function:

 jQuery.validator.addMethod("acceptPrice", function(value, element, param) {
            return value.match(new RegExp(param + "$"));
         });

    $("#editForm").validate({
          onkeyup: false,
    rules: {


        priceField: { acceptPrice: "^[a-zA-Z0-9€£]+$" },


        }

推荐答案

您应使用正则表达式模式^[a-zA-Z0-9€£]+$

You should use regex pattern ^[a-zA-Z0-9€£]+$

即使您的错误模式也不允许使用诸如;":之类的字符.您的代码中必须有一些错误,而不仅仅是正则表达式模式.

Even your wrong pattern does not allow characters such as ;, " or :. You must have some error in your code, not just in regex pattern.

更新[1]

UPDATE [1]

如果您还希望允许使用空格字符,请使用正则表达式模式^[ a-zA-Z0-9€£]+$

If you want to allow also space character, then use regex pattern ^[ a-zA-Z0-9€£]+$

更新[2]

UPDATE [2]

如果您还希望允许使用十进制数字,则还需要允许使用点号:^[. a-zA-Z0-9€£]+$

If you want to allow also decimal numbers, you need to allow dot as well: ^[. a-zA-Z0-9€£]+$

更新[3]

UPDATE [3]

JavaScript表单验证

JavaScript Form Validation

<form name="myForm" action="form.php" onsubmit="return validateForm()" method="post">

...带有脚本:

function validateForm()
{
  // if error return false;
  // if okay  return true;
}

这篇关于jQuery Regex仅允许使用货币符号和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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