Skip to main content
July 21, 2017
Question

The if and else statement

  • July 21, 2017
  • 1 reply
  • 991 views

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 = "";

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 21, 2017

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?

July 21, 2017

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!

kglad
Community Expert
Community Expert
July 21, 2017

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

}

}