Hi all,

I'd like to validate some textfields to match on alphabetic input and
whitespace only, which brought me to the RegExpValidator. (Rather long
post, please bear with me)

First of all: I'm no big hero in regular expressions. I mostly can read
one when I see one, but that's about it.
So I figured I could use the sample of the language reference to test
out my own expression (link:
http://livedocs.adobe.com/flex/2/langref/mx/validators/RegExpValidator.h\
tml#includeExamplesSummary)
The expression I came up with:
/[a-zA-Z\s]/
But as i soon discovered this only covers 1 char, not a whole input. I
moved onto /[a-zA-Z\s]*/
However... putting that expression into the sample (input = 'abcd
EFGH9023') made it lock up ("Warning: a script has been...)
Similary /[a-zA-Z\s]?/ also leads to a lockup

/[a-zA-Z\s]+/ on the other hand doesn't lock the app up, and works..
partially...
With the given input this doesn't return INVALID, but gives a valid
match upon 'abcd EFGH'.

Since I couldn't find a property/method to let RegExpValidator match
only on the complete input, I resorted to my own sample, which is as
follows:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
layout="absolute">

<mx:Script>
     <![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.validators.ValidationResult;
import mx.events.ValidationResultEvent;

private function textonlyValidation(event:FlexEvent):void
{
var vre:ValidationResultEvent =
textonlyValidator.validate(event.target.text);

if (vre.type == ValidationResultEvent.VALID) {
var match:String = vre.results[0].matchedString as String;
if (match != event.target.text) {
Alert.show("partially valid");
//hold the current event
vre.stopImmediatePropagation();
//dispatch a new one, containing an error
var vr:ValidationResult = new ValidationResult(
true,
vre.results[0].subField,
"1",
"Input contains illegal characters\nAllowed: a-z A-Z (space)");
var newVre:ValidationResultEvent = new ValidationResultEvent(
ValidationResultEvent.INVALID,
vre.bubbles,
vre.cancelable,
vre.field,
[vr]);
//dispatch it through the validator
textonlyValidator.dispatchEvent(newVre);
}
else { Alert.show("valid"); }
}
else { Alert.show("invalid"); }
}
     ]]>
</mx:Script>

<mx:TextInput x="10" y="10" id="ti"
valueCommit="textonlyValidation(event)" />

<mx:TextInput x="10" y="40" text="tab here to commit value above" />

<mx:RegExpValidator id="textonlyValidator"
     expression="[a-zA-Z\s]*" required="false" />
</mx:Application>

The idea is to catch the result of the validation, check if the match
was on the complete inputstring, and if not, replace the
validationResultEvent with one that does return invalid and thus matches
my needs.

However, this doesn't seem to be working at all.
When the input is empty, I'm getting a nullreference (probably from
event.target.text, understandable, fixable).
I have 3 more scenarios:

    1. inserting a correct value: 'valid' is alerted, so this seems to be
working fine
    2. inserting a completly wrong value ('123'): 'partially valid' is
alerted, which should be (imo) "impossible" given the normal behaviour
and matching of this regexpvalidator. This behaviour is not expected, as
the same input in the sample of the developers guide does return
invalid...
    3. inserting a mixed value: 'partially valid' is alerted, as it
should. But the newly created event holding an error doesn't seem to be
dispatched/applied to the inputfield

I'm totally open for any suggestion,
--Johan

Reply via email to