Skip to main content
August 11, 2009
Answered

Restrict property and RegExp

  • August 11, 2009
  • 1 reply
  • 990 views

Hi:

It seems it isn't possible to use RegExp with restrict property, any workaround? I received this specification for dates in an input field:

^((0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[0-2])/(19[0-9][0-9]|2[0-9][0-9][0-9]))$

Flash CS3, OS X 10.4

=================

Is it possible to intercept chars typed before they are displayed?

This topic has been closed for replies.
Correct answer Greg Dove

There are no doubt more elegant ways to do this, but I think this is the type of result you are aiming for:

var myRegExp:RegExp=/^((0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19[0-9][0-9]|2[0-9][0-9][0-9]))$/


var TF:TextField=new TextField();
TF.type="input";
TF.restrict="0-9/"
TF.height=25;
TF.border=true;
addChild(TF);

TF.addEventListener(Event.CHANGE,handleChange,false,65535);
var oldVal:String="";
function handleChange(e:Event):void{
    var test:String=TF.text+("01/01/1900").substr(TF.text.length);
    if (myRegExp.test(test)) oldVal=TF.text;
    else TF.text=oldVal;
}

stop()

above should work if pasted into a new Actionscript 3 project

1 reply

Greg DoveCorrect answer
Inspiring
August 12, 2009

There are no doubt more elegant ways to do this, but I think this is the type of result you are aiming for:

var myRegExp:RegExp=/^((0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[0-2])\/(19[0-9][0-9]|2[0-9][0-9][0-9]))$/


var TF:TextField=new TextField();
TF.type="input";
TF.restrict="0-9/"
TF.height=25;
TF.border=true;
addChild(TF);

TF.addEventListener(Event.CHANGE,handleChange,false,65535);
var oldVal:String="";
function handleChange(e:Event):void{
    var test:String=TF.text+("01/01/1900").substr(TF.text.length);
    if (myRegExp.test(test)) oldVal=TF.text;
    else TF.text=oldVal;
}

stop()

above should work if pasted into a new Actionscript 3 project

August 12, 2009

Thanks a lot Greg! that's is what I was looking for