Or question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tshad

    Or question

    I have the following:

    if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) && strYr.length>1)
    strYr=strYr.sub string(1);

    I am trying to take any blanks out of the year.

    but if "strYr = 05", it takes out the 0??????

    Is there something wrong with this statement?

    Thanks,

    Tom


  • John

    #2
    Re: Or question

    "tshad" <tscheiderich@f tsolutions.com> wrote in message
    news:8r%me.721$ wy1.113@newssvr 13.news.prodigy .com...[color=blue]
    >I have the following:
    >
    > if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) && strYr.length>1)
    > strYr=strYr.sub string(1);
    >
    > I am trying to take any blanks out of the year.
    >
    > but if "strYr = 05", it takes out the 0??????
    >
    > Is there something wrong with this statement?
    >[/color]

    Hi Tom,

    I think this will work for you.

    if (((strYr.charAt (0)<"0") || (strYr.charAt(0 )>"9")) && strYr.length>1)
    strYr=strYr.sub string(1);

    IMO, this isn't the best way to handle this. Personally, I would do a
    replace on a regular expression on non-digits. I think this would work;

    cleanString = dirtyString.rep lace( /\D/g, "");

    Good luck,
    John MacIntyre

    Specializing in; Database, Web-Applications, and Windows Software




    Comment

    • tshad

      #3
      Re: Or question

      "John" <Please.dont@se nd.me.spam.com> wrote in message
      news:6F%me.8513 $yG4.572036@new s20.bellglobal. com...[color=blue]
      > "tshad" <tscheiderich@f tsolutions.com> wrote in message
      > news:8r%me.721$ wy1.113@newssvr 13.news.prodigy .com...[color=green]
      >>I have the following:
      >>
      >> if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) &&
      >> strYr.length>1) strYr=strYr.sub string(1);
      >>
      >> I am trying to take any blanks out of the year.
      >>
      >> but if "strYr = 05", it takes out the 0??????
      >>
      >> Is there something wrong with this statement?
      >>[/color]
      >
      > Hi Tom,
      >
      > I think this will work for you.
      >
      > if (((strYr.charAt (0)<"0") || (strYr.charAt(0 )>"9")) && strYr.length>1)
      > strYr=strYr.sub string(1);[/color]

      I had just figured that out. I looked at this over and over and for some
      reason, "= 0" didn't register.

      Thanks,

      Tom[color=blue]
      >
      > IMO, this isn't the best way to handle this. Personally, I would do a
      > replace on a regular expression on non-digits. I think this would work;
      >
      > cleanString = dirtyString.rep lace( /\D/g, "");
      >
      > Good luck,
      > John MacIntyre
      > http://www.johnmacintyre.ca
      > Specializing in; Database, Web-Applications, and Windows Software
      >
      >
      >
      >[/color]


      Comment

      • Mick White

        #4
        Re: Or question

        tshad wrote:
        [color=blue]
        > I have the following:
        >
        > if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) && strYr.length>1)
        > strYr=strYr.sub string(1);
        >
        > I am trying to take any blanks out of the year.[/color]

        strYr=strYr.rep lace(/\s/g,"");

        I would test strYr further though, so that it falls within an acceptable
        range.

        [color=blue]
        >
        > but if "strYr = 05", it takes out the 0??????
        >
        > Is there something wrong with this statement?
        >[/color]

        strYr.charAt(0) <="0"
        strYr.charAt(0) >="9"
        Not sure what you are trying to accomplish here.
        Mick



        Comment

        • RobG

          #5
          Re: Or question

          tshad wrote:[color=blue]
          > I have the following:
          >
          > if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) && strYr.length>1)
          > strYr=strYr.sub string(1);
          >
          > I am trying to take any blanks out of the year.[/color]

          I guess you are getting user-entered text from a text input and
          attempting to format it into a 2 digit year. If that is correct, then
          as Mick White indicated the above is inadequate.
          [color=blue]
          >
          > but if "strYr = 05", it takes out the 0??????[/color]

          This expression will result in the variable 'strYr' being given the
          numeric value '5' and strYr will be a type number. If printed, it will
          appear as '5', not '05'.

          var y = 05;
          alert( y + ' is a ' + typeof y ) // displays '5 is a number'

          However, if you give strYr the string '05', then strYr will be a type
          string and will print as '05':

          var y = '05';
          alert( y + ' is a ' + typeof y ) // displays '05 is a string'
          [color=blue]
          >
          > Is there something wrong with this statement?[/color]

          Depends on your point of view. If strYr is a value retrieved from a
          text input it will be a string. If your intention is to ensure that
          it is a 2 digit number, then firstly:

          strYr = strYr.replace(' \D'g,'');

          will remove all non-digit characters. Subsequently you should test
          that the resulting strYr falls within a range that you consider valid.
          And note that two-digit years may still be subject to year 2000 issues.

          There are some helpful hints on date validation and formatting here:

          <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>

          Having tested that you are happy with whatever was entered, you may
          want to format strYr as a 2 digit string for output.

          Formatting is usually left until the very end, typically there isn't
          much point in bothering about it beforehand. Provided that you ensure
          that strYr is at least one digit (0-9), then the following function, if
          given a non-negative integer, will return a two digit string:

          function towDigit(x){
          return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
          }

          Numbers from 0 to 9 will have a '0' prepended, numbers greater than 99
          will have just the the last two digits returned.

          '+x' converts x to a number, removing any existing leading zero.
          Prepending a string zero '0' or empty string '' ensures the result is a
          string.

          Here is a brief implementation:

          Enter a year (0 to 99):
          <input type="text" onblur="
          var y = checkYr(this.va lue);
          alert( ((y)? 'Year is ' + y : 'Year must be 0 to 99') );
          ">

          <script type="text/javascript">
          function checkYr(y){
          y = y.replace(/\D/g,'');
          if ( '' == y ) {
          return false;
          } else {
          // Test for y within suitable range here
          // return false if it fails.
          }
          return towDigit(y);
          }

          function towDigit(x){
          return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
          }
          </script>



          --
          Rob

          Comment

          • tshad

            #6
            Re: Or question

            These are all good ideas.

            Even after figuring out that I made an error in the ">=" and "<=", I like
            some of the other approaches.

            Thanks,

            Tom
            "RobG" <rgqld@iinet.ne t.auau> wrote in message
            news:VQ7ne.1817 $Zn.87586@news. optus.net.au...[color=blue]
            > tshad wrote:[color=green]
            >> I have the following:
            >>
            >> if (((strYr.charAt (0)<="0") || (strYr.charAt(0 )>="9")) &&
            >> strYr.length>1) strYr=strYr.sub string(1);
            >>
            >> I am trying to take any blanks out of the year.[/color]
            >
            > I guess you are getting user-entered text from a text input and
            > attempting to format it into a 2 digit year. If that is correct, then
            > as Mick White indicated the above is inadequate.
            >[color=green]
            >>
            >> but if "strYr = 05", it takes out the 0??????[/color]
            >
            > This expression will result in the variable 'strYr' being given the
            > numeric value '5' and strYr will be a type number. If printed, it will
            > appear as '5', not '05'.
            >
            > var y = 05;
            > alert( y + ' is a ' + typeof y ) // displays '5 is a number'
            >
            > However, if you give strYr the string '05', then strYr will be a type
            > string and will print as '05':
            >
            > var y = '05';
            > alert( y + ' is a ' + typeof y ) // displays '05 is a string'
            >[color=green]
            >>
            >> Is there something wrong with this statement?[/color]
            >
            > Depends on your point of view. If strYr is a value retrieved from a
            > text input it will be a string. If your intention is to ensure that
            > it is a 2 digit number, then firstly:
            >
            > strYr = strYr.replace(' \D'g,'');
            >
            > will remove all non-digit characters. Subsequently you should test
            > that the resulting strYr falls within a range that you consider valid.
            > And note that two-digit years may still be subject to year 2000 issues.
            >
            > There are some helpful hints on date validation and formatting here:
            >
            > <URL:http://www.merlyn.demo n.co.uk/js-dates.htm>
            >
            > Having tested that you are happy with whatever was entered, you may
            > want to format strYr as a 2 digit string for output.
            >
            > Formatting is usually left until the very end, typically there isn't
            > much point in bothering about it beforehand. Provided that you ensure
            > that strYr is at least one digit (0-9), then the following function, if
            > given a non-negative integer, will return a two digit string:
            >
            > function towDigit(x){
            > return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
            > }
            >
            > Numbers from 0 to 9 will have a '0' prepended, numbers greater than 99
            > will have just the the last two digits returned.
            >
            > '+x' converts x to a number, removing any existing leading zero.
            > Prepending a string zero '0' or empty string '' ensures the result is a
            > string.
            >
            > Here is a brief implementation:
            >
            > Enter a year (0 to 99):
            > <input type="text" onblur="
            > var y = checkYr(this.va lue);
            > alert( ((y)? 'Year is ' + y : 'Year must be 0 to 99') );
            > ">
            >
            > <script type="text/javascript">
            > function checkYr(y){
            > y = y.replace(/\D/g,'');
            > if ( '' == y ) {
            > return false;
            > } else {
            > // Test for y within suitable range here
            > // return false if it fails.
            > }
            > return towDigit(y);
            > }
            >
            > function towDigit(x){
            > return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
            > }
            > </script>
            >
            >
            >
            > --
            > Rob[/color]


            Comment

            • Dr John Stockton

              #7
              Re: Or question

              JRS: In article <VQ7ne.1817$Zn. 87586@news.optu s.net.au>, dated Wed, 1
              Jun 2005 01:04:53, seen in news:comp.lang. javascript, RobG
              <rgqld@iinet.ne t.auau> posted :
              [color=blue]
              > Formatting is usually left until the very end, typically there isn't
              > much point in bothering about it beforehand. Provided that you ensure
              > that strYr is at least one digit (0-9), then the following function, if
              > given a non-negative integer, will return a two digit string:
              >
              > function towDigit(x){
              > return '' + ( (x<10)? '0'+ +x : x.match(/\d\d$/) );
              > }[/color]


              or return ((x%=100)<10 ? "0" : "") + x
              or return String(x+100).m atch(/\d\d$/)
              or return ((x+100)+"").ma tch(/\d\d$/)


              ISTM worth distinguishing two cases, typified by getFullYear and
              getDate.

              To get a two-digit date from getDate one need only prepend a zero of
              x<10; x is necessarily in 1..31. Best, then, to use a non-truncating
              routine, since if by programming error x is not in that range it would
              be better to see the objectionable x in full rather than something
              truncated towards plausibility.

              But to get a two-digit year from getFullYear, some form of truncation
              cannot be avoided. ISTM better to make the truncation straightforward
              and manifest as in YY = YYYY%100, after which one can proceed as above.
              And even better to use YYYY.

              Putting it differently, padding to two digits is quite distinct from
              reduction to two digits.

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...