10 Replies Latest reply on Aug 6, 2007 12:46 PM by GerryIsHere

    First two digits

    GerryIsHere Level 1
      Hi there!

      I manage a directory of former students of my school.
      Users who want to register must indicate in the form the year they entered the school.
      Alas, some of them enter bad years, like "0972" instead of "1972" or "0674" instead of "1974" !!!

      Inside a general Validation function, I already have an Array using different validating functions (one for each Field) into which I use "push" to enumerate different mistakes and prevent them with warnings and disabling of the Register Button. Like this one:

      validatePromo = function () {
      if (_root.promoField.length<1) {
      errors.push("- You MUST indicate the year you entered the school.");
      }
      if (_root. promoField.length<4) {
      errors.push("- Year must be four digits long.");
      }
      if (promotion<1925) {
      errors.push("- Enter 1925 as promotion year.");
      }
      }

      What I don't know is how to write the if statement to force user to begin the year string with 19 or 20.
      Could you please tell me ?

      Many thanks in advance,
      Regards
        • 1. Re: First two digits
          Kees_Kist Level 1
          Why don't you validate it as a number:

          function checkDate(input:String) {
          var year:Number=Number(input);
          return ((year>1900) && (year<2000));
          }
          • 2. Re: First two digits
            GerryIsHere Level 1
            quote:

            Originally posted by: Kees Kist
            Why don't you validate it as a number:

            function checkDate(input:String) {
            var year:Number=Number(input);
            return ((year>1900) && (year<2000));
            }


            Thanks for the reply.
            But this doesn't suit my needs...
            It doesn't integrate into my array.
            May be my explanation wasn't clear enough...

            What you wrote isn't good for the year 2007 by example...

            What I'm looking for is something which refers to the first two digits which MUST be 19 or 20
            • 3. Re: First two digits
              Greg Dove Level 4
              Its very close then. All you need to do is this

              function checkCentury(input:String) {
              var cent:Number=Number(input.substr(0,2));
              return ((cent>18) && (cent<21));
              }

              if (!checkCentury(_root. promoField.length)) {
              errors.push("- Century range error.");
              }
              • 4. Re: First two digits
                GerryIsHere Level 1
                Thank you very much GWD !
                I give this a try and I'll be back soon to keep you informed about the results...
                • 5. Re: First two digits
                  Kees_Kist Level 1
                  quote:

                  Originally posted by: GWD
                  Its very close then. All you need to do is this

                  function checkCentury(input:String) {
                  var cent:Number=Number(input.substr(0,2));
                  return ((cent>18) && (cent<21));
                  }

                  if (!checkCentury(_root. promoField.length)) {
                  errors.push("- Century range error.");
                  }


                  But this would also return true for the input string '19AB'. So if you would cross both of our examples you would get the desired results.
                  • 6. Re: First two digits
                    Greg Dove Level 4
                    @Kees Kist : You're right. But I think the idea here is to get a list of errors. Whether the entry is a valid number probably should be the first item in the list.

                    Probably its just better to check if the year is between 1900 (or some minimum) and the current year. It would be much simpler.
                    • 7. Re: First two digits
                      GerryIsHere Level 1
                      quote:

                      Originally posted by: Kees Kist
                      But this would also return true for the input string '19AB'. So if you would cross both of our examples you would get the desired results.

                      Nope.
                      Input string can't be 19AB because there's a restrict condition: you can enter ONLY numbers in the field...

                      • 8. Re: First two digits
                        GerryIsHere Level 1
                        quote:

                        Originally posted by: GWD
                        @Kees Kist : You're right. But I think the idea here is to get a list of errors. Whether the entry is a valid number probably should be the first item in the list.

                        Probably its just better to check if the year is between 1900 (or some minimum) and the current year. It would be much simpler.


                        You are right. The idea is to get a list of errors.
                        And when the list is empty, the register button is activated.

                        I gave your afore mentioned solution a try.
                        I voluntarily entered wrong dates like, for example, "0674".
                        I didn't get the century error ("- Century range error.") listed... even if I included
                        checkCentury();
                        in the list of the functions to be run inside my general validateForm function...

                        Any clue?

                        Anyway, thank you very much to both of you for the time you spend solving my problem!

                        • 9. Re: First two digits
                          Greg Dove Level 4
                          sorry my bad, I used copy and paste without checking

                          it should have been

                          if (!checkCentury(_root. promoField)) {
                          errors.push("- Century range error.");
                          }
                          • 10. Re: First two digits
                            GerryIsHere Level 1
                            Don't worry!
                            I've already fixed it and everything is running fine.

                            Many thanks again for your valuable help.
                            Best regards.