Copy link to clipboard
Copied
Hi,
I am making a program where a user inputs a postal code and then verify if it is valid or not. I made a code to execute it (I used the if/else statement). My code does not have any errors, but once the user enters an invalid postal code, the result is completely wrong. It seems that the result only shows the first label that I assigned, but not the other result. In order to verify the postal code, I needed to make sure that the first, third and sixth characters are capitalized. I did what I needed to do to ensure that, please check my code if there are any changes I should make.
Also, how would I identify if a certain character is a number and if a certain character is a space (no letter)?
I know I have to use str.charAt(str.indexOf("") + 1)
Here is my code:
// This line makes the button, btnCheck wait for a mouse click
// When the button is clicked, the displayCheck function is called
btnCheck.addEventListener(MouseEvent.CLICK, displayCheck);
// This line makes the textinput wait for a mouse click
// When this component is clicked, the clearLabels function is called
txtinCode.addEventListener(MouseEvent.CLICK, clearLabels);
// This is the displayCheck function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayCheck(e:MouseEvent):void
{
// declare variables
var Postal:String;
var Postal2:String;
var Postal3:String;
var str:String;
// get the string from the user
str = txtinCode.text;
// determine if the postal code entered by the user is valid or invalid
Postal = str.charAt(str.indexOf("") + 0);
Postal2 = str.charAt(str.indexOf("") + 2);
Postal3 = str.charAt(str.indexOf("") + 5);
if (Postal.toUpperCase() && Postal2.toUpperCase() + Postal3.toUpperCase())
{
lblDescription1.text = "Valid postal code.";
}
else
{
lblDescription1.text = "Invalid postal code.";
}
}
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblDescription1.text = "";
txtinCode.text = "";
}
Copy link to clipboard
Copied
that if statement does nothing even when you fix your typo. ie, this is problematic:
if (Postal.toUpperCase() && Postal2.toUpperCase() + Postal3.toUpperCase())
you probably want
if (Postal.toUpperCase() ==Postal&& Postal2.toUpperCase()==Postal2 && Postal3.toUpperCase()==Postal3)
but why not just fix the user's input yourself and have the user confirm its corrent?
Copy link to clipboard
Copied
Hello kglad,
I have input the code you have shown me and made the changes: the code works. Thank you so much! Now, going back to your question. I made the program to validate the user's input, not me. That is the purpose that I intended it to be. I guess it is much more interesting for the program to catch the errors and let the user know rather than me telling the user. I know it is much easier for me to correct the user; however, that is not the purpose of making this program.
I am so sorry if I am asking too much here. If you could just help me with these few questions I have, that would be great.
1. How would I identify if a certain character that is entered by the user is a number?
2. How would I also identify if a certain character that is entered by the user is a space?
I know I would use part of the charAt and indexOf method, that is only identifying/ locating the character, so how would I specifically say that this certain character should be a number. I know I would also use the if and else statement for this situation.
Please help me. Thank you once again!
Copy link to clipboard
Copied
1. if(!isNaN(Number(tf.text.charAt(someindex)))){ // it's a number}
2. if(tf.text.charAt(someotherindex)==' '){ // char at someotherindex is a space
if you're going to do either of those more than once, create a function. eg,:
function number_checkF(n:Number):Boolean{
if(isNaN(n)){
return false
} else {
return true
}
}
Copy link to clipboard
Copied
Hey kglad,
Your solution has helped a lot! I copied the code you told me to do, there were no errors, but the result is completely wrong. It is the same problem that I had earlier. Also, when I copied the function you provided me, it said "Function does not return a value". I know that I had to put ":void" in order to fix it, but I do not know if it is right.
Here is my edited code:
// This line makes the button, btnCheck wait for a mouse click
// When the button is clicked, the displayCheck function is called
btnCheck.addEventListener(MouseEvent.CLICK, displayCheck);
// This line makes the textinput wait for a mouse click
// When this component is clicked, the clearLabels function is called
txtinCode.addEventListener(MouseEvent.CLICK, clearLabels);
// This is the displayCheck function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayCheck(e:MouseEvent):void
{
// declare variables
var Postal:String;
var Postal2:String;
var Postal3:String;
var str:String;
// get the string from the user
str = txtinCode.text;
// selecting the first, third and sixth characters from the string
Postal = str.charAt(str.indexOf("") + 0);
Postal2 = str.charAt(str.indexOf("") + 2);
Postal3 = str.charAt(str.indexOf("") + 5);
// verify that the postal code entered by the user is valid or invalid
if (Postal.toUpperCase() == Postal && Postal2.toUpperCase() == Postal2 && Postal3.toUpperCase() == Postal3)
{
lblDescription1.text = "Valid postal code.";
}
else if (Postal.toLowerCase() == Postal)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "First character is incorrect";
}
else if (Postal2.toLowerCase() == Postal2)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Third Character is incorrect";
}
else if (Postal3.toLowerCase() == Postal3)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Sixth character is incorrect";
}
}
// This is the Number function
// validates if the second, fifth, and seventh characters entered by the user is a number
function number_checkF(n:Boolean):void
{
if (!isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") + 1))))
{
lblDescription1.text = "Valid postal code";
}
else if (!isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") + 4))))
{
lblDescription1.text = "Valid postal code.";
}
else if (!isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") + 6))))
{
lblDescription1.text = "Valid postal code.";
}
else if (isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") +1))))
{
lblDescription1.text = "Invalid postal code.";
lblDescription2.text = "The second character is not a number";
}
else if (isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") + 4))))
{
lblDescription1.text = "Invalid postal code.";
lblDescription2.text = "The fifth character is not a number";
}
else if (isNaN(Number(txtinCode.text.charAt(txtinCode.text.indexOf("") + 6))))
{
lblDescription1.text = "Invalid postal code.";
lblDescription2.text = "The seventh character is not a number";
}
}
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblDescription1.text = "";
lblDescription2.text = "";
txtinCode.text = "";
}
The result only seems to appear "Valid postal code" whenever I input an invalid postal code (Same problem as earlier). Please check the section of my code starting on "// This is the number function". Also, kindly check if my function is right. Let me know what I should change. Cannot thank you enough for this.
Copy link to clipboard
Copied
you didn't copy my code. that's why you saw an error. use:
if(code_checkF(txtinCode.text)<0){
lblDescription1.text = "Valid postal code.";
} else {
lblDescription1.text = "Invalid postal code.";
lblDescription2.text ="Character "+(code_checkF(txtinCode.text)+1)+" is not a number.";
}
function code_checkF(s:String):int{
var a:Array=[1,4,6]
for(var i:int=0;i<a.length;i++){
if(!number_checkF(s.indexOf("")+a)){
return i;
}
}
return -1;
}
function number_checkF(n:Number):Boolean{
if(isNaN(n)){
return false
} else {
return true
}
}
Copy link to clipboard
Copied
I have input the code you have shown me. The code works, there wasn't any error. However, the result is incorrect. Whenever I put an invalid postal code, it seems to keep appearing "valid postal code". Please let me know any changes.
Here's the code.
// This line makes the button, btnCheck wait for a mouse click
// When the button is clicked, the displayCheck function is called
btnCheck.addEventListener(MouseEvent.CLICK, displayCheck);
// This line makes the textinput wait for a mouse click
// When this component is clicked, the clearLabels function is called
txtinCode.addEventListener(MouseEvent.CLICK, clearLabels);
// This is the displayCheck function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayCheck(e:MouseEvent):void
{
// declare variables
var Postal:String;
var Postal2:String;
var Postal3:String;
var str:String;
// get the string from the user
str = txtinCode.text;
// selecting the first, third and sixth characters from the string
Postal = str.charAt(str.indexOf("") + 0);
Postal2 = str.charAt(str.indexOf("") + 2);
Postal3 = str.charAt(str.indexOf("") + 5);
// verify that the postal code entered by the user is valid or invalid
if (Postal.toUpperCase() == Postal && Postal2.toUpperCase() == Postal2 && Postal3.toUpperCase() == Postal3)
{
lblDescription1.text = "Valid postal code.";
}
else if (Postal.toLowerCase() == Postal)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "First character is incorrect";
}
else if (Postal2.toLowerCase() == Postal2)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Third Character is incorrect";
}
else if (Postal3.toLowerCase() == Postal3)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Sixth character is incorrect";
}
}
// this validate if the second, fifth, and seventh characters entered by the user is a number
if (code_checkF(txtinCode.text)<0){
lblDescription1.text = "Valid postal code.";
}else{
lblDescription1.text = "Invalid postal code.";
lblDescription2.text = "The second character" + (code_checkF(txtinCode.text) + 1) + "is not a number.";
}
function code_checkF(s:String):int{
var a:Array = [1,4,6]
for (var i:int = 0; i < a.length; i++){
if (!number_checkF(s.indexOf("") + a)){
return i;
}
}
return -1;
}
function number_checkF(n:Number):Boolean{
if (isNaN(n)){
return false
}else{
return true
}
}
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblDescription1.text = "";
lblDescription2.text = "";
txtinCode.text = "";
}
Copy link to clipboard
Copied
if you want to check the 2nd, 5th and 7th characters in a string to see if they're numbers, use:
function code_checkF(s: String): int {
var a: Array = [1, 4, 6]
for (var i: int = 0; i < a.length; i++) {
if (!number_checkF(Number(s.charAt(s.indexOf("") + a)))) {
return a;
}
}
return -1;
}
function number_checkF(n: Number): Boolean {
if (isNaN(n)) {
return false
} else {
return true
}
}
Copy link to clipboard
Copied
Hello kglad,
I know you have helped a lot. I am so sorry if this is going to an extent. I have exactly copied the code and did the changes you have told me, but the result seems to appear "Valid" when I input an invalid code. For instance, I am figuring out if the second, fifth, and seventh characters are numbers. I intentionally put letters instead of numbers in those specific characters to the textbox or textinput in order to test if the result will say "invalid" or "incorrect". The result does not change at all, it always appears valid.
Here's the picture for proof:
Here's the code:
// This line makes the button, btnCheck wait for a mouse click
// When the button is clicked, the displayCheck function is called
btnCheck.addEventListener(MouseEvent.CLICK, displayCheck);
// This line makes the textinput wait for a mouse click
// When this component is clicked, the clearLabels function is called
txtinCode.addEventListener(MouseEvent.CLICK, clearLabels);
// This is the displayCheck function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function displayCheck(e:MouseEvent):void
{
// declare variables
var Postal:String; // verifying if the first character is uppercase
var Postal2:String; // verifying if the third character is uppercase
var Postal3:String; // verifying if the sisxth character is uppercase
var str:String; // postal code entered by the user
// get the postal code from the user
str = txtinCode.text;
// selecting the first, third and sixth characters from the string
Postal = str.charAt(str.indexOf("") + 0);
Postal2 = str.charAt(str.indexOf("") + 2);
Postal3 = str.charAt(str.indexOf("") + 5);
// verify that the postal code entered by the user is valid or invalid
if (Postal.toUpperCase() == Postal && Postal2.toUpperCase() == Postal2 && Postal3.toUpperCase() == Postal3)
{
lblDescription1.text = "Valid postal code.";
}
else if (Postal.toLowerCase() == Postal)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "First character is incorrect";
}
else if (Postal2.toLowerCase() == Postal2)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Third Character is incorrect";
}
else if (Postal3.toLowerCase() == Postal3)
{
lblDescription1.text = "Invalid postal code";
lblDescription2.text = "Sixth character is incorrect";
}
}
// this validate if the second, fifth, and seventh characters entered by the user is a number
function code_checkF(s:String):int{
var a:Array = [1,4,6]
for (var i:int = 0; i < a.length; i++){
if (!number_checkF(s.indexOf("") + a)){
return i;
}
}
return -1;
}
function number_checkF(n:Number):Boolean{
if (isNaN(n)){
return false
}else{
return true
}
}
// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInfrput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
lblDescription1.text = "";
lblDescription2.text = "";
txtinCode.text = "";
}
Copy link to clipboard
Copied
1. you're not using any of the code i suggested. at least, i can't see any place where you're calling code_checkF
2. what the point of str.indexOf("") in your code?
3. what country's postal code are you trying to validate?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now