加号可以打破正则表达式验证

时间:2014-03-13 09:39:57

标签: python regex validation

我有很多正则表达式,例如:

quantity_validation = re.compile("""^-?[0-9]+$""", re.I)

我在以下函数中使用这些正则表达式:

def validate_quantity(self, value):
    if value != self.context.quantity:
        # while not self.quantity_validation.match(value):
        #     return 0, "Value is not a number"
        if not self.quantity_validation.match(value):
            return 0, "Value is not a number"
    return 1, ""

和验证方法:

@view_config(name="validate", renderer='json')
def validate(self, full={}):
    def do_validation(field, value):
        message = ""
        valid = 1
        if getattr(self, 'validate_%s' % field, False):
            valid, message = getattr(self, 'validate_%s' % field)(value)

        out = dict(message=message, valid=valid, value=value)
        return out
    if not full:
        field = self.request.params.get('field')
        if not field:
            return "INVALID"
        return do_validation(field, self.request.params.get('value'))
    else:
        return dict(((field, do_validation(field, value)) for field, value in full.items()))

在HTML表单上,我有一个带有quantity字段的模态,我在其上进行ajax验证(我使用jqBootstrapValidation来验证我的表单)

正则表达式验证工作正常,直到我输入+,然后验证停止工作,直到我刷新页面。

为什么验证停止工作?
它可能是jqBootstrapValidation中的一个错误吗? 或者它是re module中的错误?

1 个答案:

答案 0 :(得分:1)

您没有正确编码您的网址/输入。通过参数字符串传递时,'+'会被解析为' '。请尝试'%2B',而应通过表单库将其解码为'+'

相关问题