Results 1 to 12 of 12
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string testing in Javascript

    Sigh... back to Javascript! <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>

    I am trying to test for an inputted <font face="Georgia">year</font face=georgia> and can't get the @#$!% to work. with Script Debugging active and 'Display a notification about every script error' enabled, I get 'object does not support this property or method' when trying to use <font face="Georgia">substring</font face=georgia> or <font face="Georgia">substr</font face=georgia> to extract some characters from a part of a string. Drat!!

    What I am TRYING to do is, having a variable already defined to hold the value for a year, I want to extract the first two numbers (characters) from this variable and test whether they are anything other than '19' or '20'. So, I thought of using Substring as that seems appropriate.

    Of course, Javascript would rather die than cooperate.

    here's some of the code:
    <font face="Georgia">
    var month = parseInt(elems[0],10);
    var day = parseInt(elems[1],10);
    var year = parseInt(elems[2],10);
    result = !isNaN(month) && (month > 0) && (month < 13) &&
    !isNaN(day) && (day > 0) && (day < 32) &&
    !isNaN(year) && (elems[2].length == 4);

    var yearTest = year;
    alert(yearTest.substr(0,2));
    alert(yearTest);
    </font face=georgia>
    As you might notice, var year contains the 4 digits for year assembled into the full date string result. doing alert(year) gives me the needed string; doing year.substring(x,y) or year.substr(x,y) gives me squat. OK, an error, but who needs that?

    ???
    <img src=/S/confused.gif border=0 alt=confused width=15 height=20>
    TIA

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: string testing in Javascript

    You told JavaScript that year is a variant of type integer (sort of, indirectly), so that's probably why can't take its substring. Try casting yearTest to a string this way:

    <big>var yearTest = "" + year;</big>

    I don't know where I picked that up, but this would be the first time I've used it. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    actually with some further research I managed to squeeze out this code:
    <font face="Georgia">
    var Today = new Date()
    var yearTest = Today.getYear();

    if (year < 1899)
    {
    alert('The year for ' + fieldLabel + ' must be later than 1899.')
    formField.focus();
    return false;
    }

    if (year > yearTest)
    {
    alert('The year for ' + fieldLabel + ' must be earlier than or equal to ' + yearTest + '.')
    formField.focus();
    return false;
    }
    </font face=georgia>
    Which does most of what I want: the var <font face="Georgia">year</font face=georgia> is defined in terms of a parsed string and the variable <font face="Georgia">yearTest</font face=georgia> holds the year part of the date object. I can then test if the textbox in question shows year before 1899 or later than 2004 (or current year in the system clock). Not perfect, because you can still load a date value for some time later this year but it's better than it was before. the next step will be to do the same thing for the day and month...

    What my research has come up with is, the object <font face="Georgia">new Date()</font face=georgia> instanciates the Javascript Object for Date/Time; I suspect that this is an object that is based on UMT (I think that's the TLA <img src=/S/grin.gif border=0 alt=grin width=15 height=15> ) and the format is something that is built in to the Javascript object Date(). IOW, I suppose the actual string you get from Date() isn't *really* a string, so you can't do parsing as you might expect. A little unfortunate! I suppose you could call an instance of the date and pass it to some other function that converts it to string from which you can do all the parsing desired... dunno.

    I'm trying to keep a date field in a simple text box rather than goofy drop-downs. This seems to be yet another instance where making something useful for the user makes life difficult for the programmer. <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    However, the variable <font face="Georgia">year</font face=georgia> isn't derived from Date() so I'm still puzzled that I can't just inspect the first two digits and ring alarms if they aren't <font face="Georgia">19 || 20</font face=georgia>.

    Onward thru the fog!

  4. #4
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    well i tried your suggestion to assign a var with '''' in front. what's that supposed to do??

    I tried using the getDay() and getMonth() objects of the new Date() Javascript function and that doesn't work very well at all. turns out getDay() returns the number of the day in terms of a week (Sunday=0) and the getMonty() returns the month number on a Zero index (January = 0). Sigh...

    BUT, if you do get Date() you end up with the string
    <font face="Georgia">Wed May 12 15:26:27 CDT 2004</font face=georgia>
    SO! If I can 'cast' this result into a simple string I can parse it to extract Month and Date as needed. So I tried your odd suggestion. Didn't work! How's it supposed to, anyhow?

    I tried re-defining the result of the Date() in a variable into a new variable and parsing that, but Javascript just dies when it sees it. I may just settle for not allowing entries greater than the current year. Better than nuttin.

    I REALLY need to get a book! <img src=/S/compute.gif border=0 alt=compute width=40 height=20>

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: string testing in Javascript

    You've moved the problem. <img src=/S/smile.gif border=0 alt=smile width=15 height=15> If the variable year is 95, it alerts that the year must be greater than 1899. The comparison seems to be casting the string 95 as an integer. Are you enforcing a 4 digit year date somewhere else in your code to avoid this particular situation?

    > I'm still puzzled that I can't just inspect the first two digits and ring alarms if they aren't 19 || 20.

    You should be able:

    <pre>var year = "95"
    if (year.length > 1)
    {
    if ((year.substring(0,2) != "19") & (year.substring(0,2) != "20"))
    {
    document.write("Give me the whole 4 digits; I don't like " + year.substring(0,2));
    }
    else
    {
    document.write("Thanks!");
    }

    }
    else
    {
    document.write("Year too short!!!");
    }</pre>

    Maybe I'm missing something.....

  6. #6
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    Try Date().toString()

    You can use the .toString() to convert/cast most any variable to a string.

  7. #7
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    there is a block of code above the 1899/after current year test that bars non-4 digit numbers.

    Sometimes moving the problem feels an aweful lot like progress!

    but, with the prior test to ban 2-digit years the current testing works alright. I can't test thoroughly for a date after today but before the next year but there's always hope...

  8. #8
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    i'm trying to use this but nothing seems to be happening:
    <font face="Georgia">
    var Today = new Date()
    var yearTest = Today.getYear();
    var dateTest = new Date().toString();
    //alert(dateTest);
    var monthTest = substr.dateTest(4,3);
    alert(monthTest);
    </font face=georgia>
    If you uncomment the first alert you get the usual long date information. the second alert never fires; considering this is javascript that probably means that the execution died when it tried to run the substr() function. With 'Display for every script error' ticked I get no errors. I've also tried putting Today thru conversion to string with the hope of parsing and that did diddly too.

    errrgh! <img src=/S/hmmn.gif border=0 alt=hmmn width=15 height=15>

    any ideas??

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: string testing in Javascript

    > substr.dateTest(4,3);

    That's inside out. Doesn't it throw a script error?

  10. #10
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    I believe the syntax is fine. substr() is supposed to take the first number as the place to start and the second number as the length of the section to grab. at any rate, i don't get an error when trying to use it, just no results.

  11. #11
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: string testing in Javascript

    Let me try again, as inside-out wasn't a proper technical description of the issue:

    > substr.dateTest(4,3);

    You surely meant:

    dateTest.substr(4,3);

  12. #12
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: string testing in Javascript

    i suppose -- beats me! i wasn't getting much response from Javascript on trying to use substr(). My impression of Javascript is it will die at the least sign of trouble, and you may or may not get an error message as it expires. Also, FrontPage can't get the line number right on Javascript code (seems to be off by one) so coding for Javascript can be an exercise in snark hunting. Ahh well... <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

    Anyhow, I fixed my validation problem and i didn't have to do anything with parseing the new Date() result. Turns out, as sometimes is the case, I was getting WAAAY more complicated than I needed to be. After another look at the overall problem I got my results with some simple if/then tests and a few other things to massage new Date() to get the info I needed. The resulting date validation does the following:

    1) enforces MM/DD/YYYY formatting (including the /slashes/)
    2) if your month or date is out of range you get a warning (it's not smart enough to deal with leap years and will allow you to input up to 31 for any month, so it's not perfect)
    3) if the year is before 1900 an alert pops up
    4) if the year is later than this year an alert pops up
    5) if the date is in this year but later than Today an alert pops up

    Pretty fair! This is all going into an Access db and the field is set up as Text (NOT Date/Time) so the smaller potential inaccuracies won't bomb the system. It seems, overall, to be 'good enough for government work.'

    And now I can go on to apply a lot of other little fixes/tweaks. I don't like abandoning a problem and it's a relief to have basically fixed this one.

    Thanks for all the help, tho!

    <img src=/S/flatcat.gif border=0 alt=flatcat width=61 height=21>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •