[Javascript] chopping up a string?

Paul McGuire pmcguire at cguk.co.uk
Mon Nov 17 09:11:47 CST 2003


It is as the field looses focus I have been working on this all day and my
solution (which is still theory) is

a function coded so that it checks for the presence of
the "/" character in the inputed field. You can use indexOf() in JS to do
this. If there is not
one in the string it may well be that a 1711 or 17 has been
entered instead of a "proper date format". So we will assume the string
entered
needs to be converted - for example 1711 to 17/11/03

if we divide the input by 2 we will get the number of dateparts in the
string - the number of characters in the string "17" divides by 2 once so we
know there is only a day of month in that string. The 17th. characters in
"1711" divides by 2, 2 times, so we can assume we have a day of month and a
month -17th of November in this example

the JavaScript switch statement can then be used now to start building the
full date. Its a case statement but is called switch in JS.

e.g

amountofdateparts = ourdatestring / 2
switch(amountofdateparts)

case 1
ourdatestring = urdatestring  + "/"

case 2
ourday =  ourdatestring.substring(0,2)
ourmonth =  ourdatestring.substring(2,3)
ourdatestring = ourday + "/" + ourmonth + "/"

there might be a situation in which 171103 is entered you could split this
like in case 2 but in case 3 because 171103 (6 characters) divides by 2, 3
times - of course.

once the case statement exits you either have 17/ or 17/11/ in the
ourdatestring variable.

you then need to get the current date strip of the amount of characters in
ourdatestring and concatenate it to ourdatestring

i.e

current date is 17/11/03

oursatestring is 17/11/

strip the 6 characters off current date to leave 03

17/11/ + 03

and there you have it the date in a variable created from the numbers added
into the reqdate box.




am I on the right lines? these are my own notes I wrote earlier when working
out a way. have only just started coding it. The dates are in British format
but it does not have to create true dates as I then will pass it into a date
check function


Paul

----- Original Message ----- 
From: "Walter Torres" <walter at torres.ws>
To: "[JavaScript List]" <javascript at LaTech.edu>
Sent: Monday, November 17, 2003 3:03 PM
Subject: RE: [Javascript] chopping up a string?


> Do you want this...
>  - on the fly (as they type)
>  - after the field losses focus
>  - modify the data just before the form is submitted
>
> Questions, questions.
>
> I have code for the last, still fiddling with code for the first.
>
> Walter
>
>
>
> > -----Original Message-----
> > From: javascript-bounces at LaTech.edu
> > [mailto:javascript-bounces at LaTech.edu]On Behalf Of Paul McGuire
> > Sent: Monday, November 17, 2003 4:06 AM
> > To: [JavaScript List]
> > Subject: [Javascript] chopping up a string?
> >
> >
> > Hi Guys n gals,
> >
> > I need to be able to break up a numeric form field in JS and turn
> > it into a
> > date format for example:
> >
> > someone enters 10 into the text box, the JavaScript needs to change it
to
> > 10/10/03 (or whatever the current date is!)
> >
> > likewise if they enter 1010 the JS should change it to 10/10/03 etc.
> >
> > I am familiar with capyuring the data and passing it to a
> > function and then
> > passing it back to the text field. However counting andchopping up the
> > string is something  I cant do. if 1010 is entered I need to know how to
> > count there are 4 numbers split them into10/10 and grab the
> > current year and
> > add /03 to the end.
> >
> > Any Help would be apreciated.
> >
> > Paul
> >
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list