RegExpValidator 从不匹配 [英] RegExpValidator never matches

查看:16
本文介绍了RegExpValidator 从不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类用于验证输入字段以确保值始终为小数.我在这里测试了正则表达式:http://livedocs.adobe.com/flex/3/html/help.html?content=validators_7.html,看起来它做对了,但在我的应用程序中,我似乎无法做到匹配数字格式.

I've got a class that's meant to validate input fields to make sure the value is always a decimal. I've tested the regex here: http://livedocs.adobe.com/flex/3/html/help.html?content=validators_7.html, and it looks like it does the right thing, but in my app, I can't seem to get it to match to a number format.

类定义:

public class DecimalValidator {
    //------------------------------- ATTRIBUTES
    public var isDecimalValidator:RegExpValidator;

    //------------------------------- CONSTRUCTORS
    public function DecimalValidator() { 
        isDecimalValidator                  = new RegExpValidator();
        isDecimalValidator.expression       = "^-?(\d+\.\d*|\.\d+)$";
        isDecimalValidator.flags            = "g";
        isDecimalValidator.required         = true;
        isDecimalValidator.property         = "text";
        isDecimalValidator.triggerEvent     = FocusEvent.FOCUS_OUT;
        isDecimalValidator.noMatchError     = "Float Expected";
    }

}

在此处设置来源:

public function registerDecimalInputValidator(inputBox:TextInput, valArr:Array):void  {
        // Add Validators
        var dValidator:DecimalValidator = new DecimalValidator();
        dValidator.isDecimalValidator.source = inputBox;
        dValidator.isDecimalValidator.trigger = inputBox;

        inputBox.restrict = "[0-9].\\.\\-";
        inputBox.maxChars = 10;

        valArr.push(dValidator.isDecimalValidator);
    }

在这里调用它:

registerDecimalInputValidator(textInput, validatorArr);

其中 textInput 是之前创建的输入框.

Where textInput is an input box created earlier.

显然我遗漏了一些简单但重要的东西,但我不完全确定是什么!任何帮助将不胜感激.

Clearly I'm missing something simple yet important, but I'm not entirely sure what! Any help would be much appreciated.

推荐答案

我不知道 ActionScript,但据我所知它是一种 ECMAScript 语言,所以我希望如果您使用字符串来转义反斜杠定义一个正则表达式:

I don't know ActionScript, but as far as I know it's an ECMAScript language, so I expect you need to escape the backslashes if you use a string to define a regex:

isDecimalValidator.expression       = "^-?(\\d+\\.\\d*|\\.\\d+)$";

这篇关于RegExpValidator 从不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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