validation check for text and numbers

Actionscript 3.0 questions.

validation check for text and numbers

Postby shariffa » Mon Apr 02, 2012 8:19 am

Hi everyone

I have a problem. I'm new at AS3 and i need alot of help.
I have a scenario here. I have 2 fields which i require the users to key in. 1) Name 2) Telephone No.
How do I do a check if the characters keyed in are at least 5 characters long and must only consist of A-Z and a-z for the Username field.
And as for the telephone no, the field to only have 8 digits and no other characters such as letters and symbols.
And all error msgs are displayed in another ladel next to each text field.
I have created the fields but i'm not sure on how to go about doing the validation checks.

Code: Select all
//text field for Name
var myFormat1:TextFormat = new TextFormat();
var myFormat1a:TextFormat= new TextFormat();
var myFormat1error:TextFormat= new TextFormat();

myFormat1.size=15;
myFormat1a.size=15;
myFormat1error.size=15;
myFormat1.align=TextFormatAlign.CENTER;
myFormat1a.align=TextFormatAlign.CENTER;
myFormat1error.align=TextFormatAlign.CENTER;

var myText1a:TextField = new TextField();
myText1a.defaultTextFormat=myFormat1a;
myText1a.text="Name:";
addChild(myText1a);

var myText1:TextField = new TextField();
myText1.defaultTextFormat=myFormat1;
myText1.text="Type your name here";
addChild(myText1);

var myText1error:TextField = new TextField();
myText1error.defaultTextFormat=myFormat1a;
myText1error.text="Error appears here";
addChild(myText1error);


myText1.border=true;
myText1.wordWrap=true;
myText1.width=150;
myText1.height=20;
myText1.x=150;
myText1.y=100;
myText1.type="input";

myText1a.width=150;
myText1a.height=20;
myText1a.x=50;
myText1a.y=100;

myText1error.width=150;
myText1error.height=20;
myText1error.x=290;
myText1error.y=100;

//telephone field for Name
var myFormat2:TextFormat = new TextFormat();
var myFormat2a:TextFormat= new TextFormat();
var myFormat2error:TextFormat= new TextFormat();

myFormat2.size=15;
myFormat2a.size=15;
myFormat2error.size=15;

myFormat2.align=TextFormatAlign.CENTER;
myFormat2a.align=TextFormatAlign.CENTER;
myFormat2error.align=TextFormatAlign.CENTER;

var myText2a:TextField = new TextField();
myText2a.defaultTextFormat=myFormat2a;
myText2a.text="Telephone No:";
addChild(myText2a);

var myText2:TextField = new TextField();
myText2.defaultTextFormat=myFormat2;
myText2.text="Type your Telephone No here";
addChild(myText2);

var myText2error:TextField = new TextField();
myText2error.defaultTextFormat=myFormat2;
myText2error.text="Error appears here";
addChild(myText2error);


myText2.border=true;
myText2.wordWrap=true;
myText2.width=150;
myText2.height=20;
myText2.x=200;
myText2.y=150;
myText2.type="input";

myText2a.width=300;
myText2a.height=40;
myText2a.x=1;
myText2a.y=150;

myText2error.width=300;
myText2error.height=40;
myText2error.x=270;
myText2error.y=150;


Could someone help me out please. Much Appreciated!
shariffa
 
Posts: 4
Joined: Thu Mar 29, 2012 1:44 pm
Flash Version: Adobe Flash CS3

Re: validation check for text and numbers

Postby BiffCornwall » Tue Apr 03, 2012 7:04 pm

Hi! Adobe has some great documentation that you should read through, which you can find here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html.

It contains answers to each of the questions you asked here. That being said, what you're wanting to do is pretty easy.

You can restrict the characters that can be typed into an input textfield very easily by using the restrict property of the TextField class. So, if you wanted to restrict a textfield to just upper and lowercase letters, simply do the following:
Code: Select all
myText1.restrict = "A-Za-z";

You can similarly use the restrict method to restrict your textfield to only numbers, like so:
Code: Select all
myText2.restrict = "0-9";

Finally, if you'd like to set the maximum amount of characters that can be allowed in a textfield, you can use the maxChars property:
Code: Select all
myText2.maxChars = 8;

Hope this helps!
BiffCornwall
 
Posts: 15
Joined: Tue Jun 15, 2010 6:49 pm
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows

Re: validation check for text and numbers

Postby shariffa » Wed Apr 04, 2012 4:46 am

Hi

Ok i have tried out the codes but there seems to be a problem. Could you help?

Code: Select all

//validation for name field
function clickHandler(event:MouseEvent)void{

if(myText1.text.length < 5){
// allows for min 5 characters
trace(myText1error.text = "Min 5 characters, Thank you");
}//only alphabets allowed
if (myText2.text.restrict = "A-Za-z")
{
trace(myText2error.text = "Alphabets only");
}
else{
trace(myText1error.text = "Thank you!");
}   

//validations for telephone no field
if (myText2.text.length >8){
   
//only allows 8 digits
   trace (myText2error.text = "Limited to only 8 charactors");
}//only allows numbers
if (myText2.text.restrict = "0-9")
{
trace(myText2error.text = "Numbers only");
}
else{
   trace(myText2error.text = "Thank you");
}
}

button_enter.addEventListener(MouseEvent.CLICK,clickHandler);
shariffa
 
Posts: 4
Joined: Thu Mar 29, 2012 1:44 pm
Flash Version: Adobe Flash CS3

Re: validation check for text and numbers

Postby BiffCornwall » Fri Apr 06, 2012 3:53 pm

Hi! Ok so there's many problems with the code you posted. I would really suggest you look into purchasing an ActionScript 3.0 book that will help you get a better overall understanding of the language. I would suggest Essential Actionscript 3.0. It's a good beginner book that will get you a long way.

If you set the restrict property to "A-Za-z" you do NOT have to check for other characters, as the textfield will not allow any other characters to be typed into the field. Similarly, if you set the restrict property to "0-9" you do NOT have to check for other characters, as the textfield will not allow any other characters to be typed into the field. This same kind of thinking also carries over to the maxChars property. If you set the maxChars property to 8, you do NOT have to verify that there are no more than 8 characters in the textfield, as setting this property makes it IMPOSSIBLE for the user to enter more than 8 characters into the textfield.

You're also attempting to verify that the same textfield (myText2) has a restriction set for allowing only A-Za-z AND 0-9. The same textfield can't be both, unless you explicitly set the restrict property to allow for only both, like so: myText2.restrict = "A-Za-z0-9". However, I don't think this is what you meant to accomplish.

You also have problems with your if statements, because you must use two equal signs to evaluate a statement. Using one equal sign simply sets the value of the second to the property of the first.

I cannot stress enough the importance of spending time going through the documentation at the link I provided in my first post. In doing so, you will have a much better understanding of these properties and how they are used.

Do this:
Code: Select all
myText1.restrict = "A-Za-z";
myText2.restrict = "0-9";
myText2.maxChars = 8;

If you do the above when your program first runs, there will be no need for any verification for character restrictions or character counts.
BiffCornwall
 
Posts: 15
Joined: Tue Jun 15, 2010 6:49 pm
Flash Version: Adobe Flash CS5.5
Authoring Environment: Microsoft Windows


Return to Actionscript 3.0

Who is online

Users browsing this forum: No registered users and 2 guests