12/21/2009 2:34:33 AM
Title:
validators
hi.. can i validate the string in flex: that means i have one name text box the starting enter is not in number when i m enter the name in the box..i can start the name in alphabet after that we can add number or anything....can i do it flex 3? eg: name:am2233th ///i want this name:12amutha /// not this
12/21/2009 4:06:04 AM
You can do it like this, check the first character on change event of textbox. If its number display an error that its invalid data.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
<mx:Script>
<![CDATA[
private function checkText():void{
var txtFirstChar:String= txtBox.text.charAt(0);
if(Number(txtFirstChar) > 0){
trace("invalid characer")
}
}
]]>
</mx:Script>
<mx:TextInput x="119" y="29" id="txtBox" change="checkText()"/>
</mx:Application>
12/21/2009 4:28:14 AM
Thanks for ur replay... but its not working... i found the solution ...below code works perfectly....
<mx:TextInput id="ti" restrict="[a-z][A-Z]" change="onChange(event)"/> private function onChange(event:Event):void { if(ti.text.length > 0) ti.restrict = "[0-9][a-z][A-Z]"; else ti.restrict = "[a-z][A-Z]" }
12/21/2009 4:32:09 AM
Your code using regix to restrict textbox is better good :)
12/21/2009 11:16:00 PM
i have an another problem in the above code ... when i m entering the number first its work very well... once i enter the number ,move the cursor to the first position on the time i can enter the number...how can i avoid this problem?
12/22/2009 1:52:24 AM
u are just checking when first character is entered and after that the first charater is never checked. because length is greater. use this code instead , i am combining your and Hindonio's code check on every change event whether first character is number if yes then it will restrict // assuming your textbox id is ti
var txtFirstChar:String= ti.text.charAt(0); if(Number(txtFirstChar) > 0){ ti.restrict = "[a-z][A-Z]"; else ti.restrict = "[0-9][a-z][A-Z]"; }
12/22/2009 2:23:19 AM
thanks for ur reply i tried ur code its working but once i entered the name and move the cursor to the starting position in text input one time i can enter the number after that i cannt do it... how can i avoid this? example: name:amutha565765 immediately move my cursor point to starting position :1amutha565765//how to avoid it
12/22/2009 2:47:17 AM
instead of change event use keydown event instead so that it checks as the key is pressed or you can add a line to splice first charcter of textbox before restrict line. //write here code to remove first character from textbox
ti.restrict = "[a-z][A-Z]";