开发者

RegExpValidator MXML

Hi

I have a problem with a regular expression. I have an RegExpValidator in MXML and I want that is invalid when the source contain a or b

My RegExpValidator is

<mx:RegExpValidator source="{value}"
                    property="text"
                    expression='.*[^ab].*'
                    valid="isValid(event)"
                    invalid="isInvalid(event)"/>

My expression is expression='.*[^ab].*' When it's just a, b or a and b (one or many times) the expression is invalid : OK

When it's everything else the expression is valid : OK

But when it a or/and b with other caractere, it's also valid. What do I have to chan开发者_StackOverflow中文版ge to have this invalid?


Imagine the string abc. If you apply the regex .*[^ab].* to it, the first .* will match ab, the [^ab] matches c, and the final .* matches the empty string.

Also, if you don't anchor your regex to the start and end of the string, it might happen (depending on the implementation of your validator) that the regex declares success if only a substring matches.

You want this:

^[^ab]*$

This matches any number of characters except a or b. ^ anchors the regex to the start, $ to the end of the string.


There are a lot of online tools that can help you with finding the right RegExp. Some of them might take you a while to perfect :P

The one I mostly use is this one: http://gskinner.com/RegExr/

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜