Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Text and Date Manipulation

Reply
Thread Tools

Text and Date Manipulation

 
 
sgershon@gmail.com
Guest
Posts: n/a
 
      03-14-2005
Hi.

I know this is should be a simple question.
I know server-side web-programming, and never needed to use client-side
scripting... until now

I have done so far a little number of scripts that work well. But there
are two that I am having special difficulties with:

1)
I have a SELECT type input, "document.form_name.select_input".
I have a TEXTAREA type input, "document.form_name.text_area".
I have a BUTTON "button".
When "button" is pressed, "text_area"s value should be
"text_area"+"select_input"s value.

2)
I have a TEXT type input "document.form_name.date1".
I have a TEXT type input "document.form_name.date2".
I have a TEXT type input "document.form_name.days".
When any of the first two changes (to a date in the format
"dd/mm/yyyy"), the third one should contain the difference between the
dates, in days.

Any help with these?

Thanks;
SGershon

 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      03-14-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi.
>
> I know this is should be a simple question.
> I know server-side web-programming, and never needed to use client-side
> scripting... until now
>
> I have done so far a little number of scripts that work well. But there
> are two that I am having special difficulties with:
>
> 1)
> I have a SELECT type input, "document.form_name.select_input".
> I have a TEXTAREA type input, "document.form_name.text_area".
> I have a BUTTON "button".
> When "button" is pressed, "text_area"s value should be
> "text_area"+"select_input"s value.
>
> 2)
> I have a TEXT type input "document.form_name.date1".
> I have a TEXT type input "document.form_name.date2".
> I have a TEXT type input "document.form_name.days".
> When any of the first two changes (to a date in the format
> "dd/mm/yyyy"), the third one should contain the difference between the
> dates, in days.
>
> Any help with these?
>
> Thanks;
> SGershon
>


It sounds like a school assignment.


 
Reply With Quote
 
 
 
 
kaeli
Guest
Posts: n/a
 
      03-14-2005
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) enlightened us with...
>
> I have done so far a little number of scripts that work well. But there
> are two that I am having special difficulties with:


Neither are very hard.
Are you sure this isn't homework?

>
> 1)
> I have a SELECT type input, "document.form_name.select_input".
> I have a TEXTAREA type input, "document.form_name.text_area".
> I have a BUTTON "button".
> When "button" is pressed, "text_area"s value should be
> "text_area"+"select_input"s value.
>


Hints:
-- onclick
-- document.form_name.select_input.options
[document.form_name.select_input.selectedIndex].value
-- document.form_name.text_area.value
-- NaN / isNaN

> 2)
> I have a TEXT type input "document.form_name.date1".
> I have a TEXT type input "document.form_name.date2".
> I have a TEXT type input "document.form_name.days".
> When any of the first two changes (to a date in the format
> "dd/mm/yyyy"), the third one should contain the difference between the
> dates, in days.


Hints:
-- onChange
-- http://www.w3schools.com/js/js_obj_date.asp

HTH

--
--
~kaeli~
If you don't pay your exorcist, you get repossessed.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
SGershon
Guest
Posts: n/a
 
      03-14-2005

No, it is not a school homework. I can see that the question is written
as one, maybe cause I used to teach in a kinder-garden...

I am 26 years old, so if it was a school assignment, my teacher would
be real mad at me! lol!

I have done simple pages on HTML/PHP, and went very well without
JavaScript (I know its powerfull and has lots of capabilities). Now one
of the scripts I need to do has to change many things dynamically
before submitting it. Many of the functions I've already done and they
are working, but these two keep giving me errors or wrong results.

Thanks for the education concern, anyway!
And thanks for any help you can give me.

 
Reply With Quote
 
kaeli
Guest
Posts: n/a
 
      03-14-2005
In article <(E-Mail Removed)>,
(E-Mail Removed) enlightened us with...
> In article <(E-Mail Removed) .com>,
> (E-Mail Removed) enlightened us with...
> >
> > I have done so far a little number of scripts that work well. But there
> > are two that I am having special difficulties with:

>
> Neither are very hard.
> Are you sure this isn't homework?
>


Oh, if it IS homework and you want it completed, just sent me $30 (I take
Paypal).
I'll be happy to do your work for you.

--
--
~kaeli~
If a turtle doesn't have a shell, is he homeless or naked?
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      03-14-2005
(E-Mail Removed) wrote:
> Hi.
>
> I know this is should be a simple question.
> I know server-side web-programming, and never needed to use client-side
> scripting... until now
>
> I have done so far a little number of scripts that work well. But there
> are two that I am having special difficulties with:
>
> 1)
> I have a SELECT type input, "document.form_name.select_input".
> I have a TEXTAREA type input, "document.form_name.text_area".
> I have a BUTTON "button".
> When "button" is pressed, "text_area"s value should be
> "text_area"+"select_input"s value.
>
> 2)
> I have a TEXT type input "document.form_name.date1".
> I have a TEXT type input "document.form_name.date2".
> I have a TEXT type input "document.form_name.days".
> When any of the first two changes (to a date in the format
> "dd/mm/yyyy"), the third one should contain the difference between the
> dates, in days.


Such an endeavour is fraught with danger; relying on a user to enter the
correct format, that is.

days.value= Math.floor ((new Date (date1.value)-new Date
(date2.value))/(1000*60*60*24));

Need some error checking, though.
Mick




 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      03-14-2005
"SGershon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> No, it is not a school homework. I can see that the question is written
> as one, maybe cause I used to teach in a kinder-garden...
>
> I am 26 years old, so if it was a school assignment, my teacher would
> be real mad at me! lol!
>
> I have done simple pages on HTML/PHP, and went very well without
> JavaScript (I know its powerfull and has lots of capabilities). Now one
> of the scripts I need to do has to change many things dynamically
> before submitting it. Many of the functions I've already done and they
> are working, but these two keep giving me errors or wrong results.
>
> Thanks for the education concern, anyway!
> And thanks for any help you can give me.
>


Will this get you started? Watch for word-wrap.

This uses Mick White's formula for "days".

<html>
<head>
<title>SGershon.htm</title>
<script type="text/javascript">
function append(form) {
var what =
form.select_input.options[form.select_input.selectedIndex].value;
form.text_area.value += what;
}
function calc(form) {
var dat1 = form.date1.value;
if (dat1.length != 10) return;
var dat2 = form.date2.value;
if (dat2.length != 10) return;
form.days.value = Math.floor((new Date(dat2)-new
Date(dat1))/(1000*60*60*24));
}
</script>
</head>
<body>
<form>
<select name="select_input">
<option value="1">1
<option value="2">2
</select>
<textarea name="text_area" cols="30" rows="10"></textarea>
<input type="button" onclick="append(this.form)">
<br><br>
<input type="text" name="date1" size="10" maxlength="10"
onchange="calc(this.form)">
<input type="text" name="date2" size="10" maxlength="10"
onchange="calc(this.form)">
<input type="text" name="days" size="5" maxlength="6" readonly> days
difference
</form>
</body>
</html>



 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-14-2005
(E-Mail Removed) wrote:
> Hi.
>
> I know this is should be a simple question.


Questions are easy, it's the answers that cause headaches

[...]
> I have done so far a little number of scripts that work well. But there
> are two that I am having special difficulties with:

[...]

The practical part of your question (getting the content of form
elements and doing something with it) has been answered above.
The remaining issue is how to reliably capture dates entered by
a user.

You have two options:

1. Allow them to enter text and validate it thoroughly

2. Present a 'fool-proof' date picker that will only allow valid
dates to be entered.

The choice between the two depends on your users. If they are
entering lots of dates or reasonably computer and keyboard
literate, they will prefer to use option 1. If they are casual
users who only need to enter single dates infrequently, option
2 may suit better (or offer both).

Finally, never trust user-entered dates and always check the
content at the server.

Since no one has pointed you at Dr J's site for date stuff, here
it is:

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

Take the time to read and understand the issues here, I think
you'll find whatever you want.

A last word on date formats: the primary ISO standard is
yyyy-mm-dd, however other formats are suggested also.

Please read an excellent discussion of ISO8601 date/time formats
(recommended by the w3c) by Markus Kuhn here:

<URL:http://www.cl.cam.ac.uk/~mgk25/iso-time.html>


--
Rob
 
Reply With Quote
 
SGershon
Guest
Posts: n/a
 
      03-15-2005
Thanks McKirahan, for your help.
Thanks to everbody else that cared to reply as well.

The code you suggested worked perfectly by itself, but then I had a
little problems when using it in my page.
After a while, I managed to get the problems sorted.
The append is appending, and the days calculator is calculating.

I have a question on each function:
1) In the end of the append function, I do an text_area.focus(). It is
focusing, but the cursor stays at the first character.
How can I move the cursos to the end in order to make the user's work
easier (faster)?

2) I am having much trouble trying to use the dd/mm/yyyy format in the
date function.
Where can I set the "new date()" to use this format? I learned a
devguru.com that when building a date object from a string as we are
doing, it reads it in the format date.parse() tells. But I cannot see a
way to change from mm/dd/yyyy to dd/mm/yyyy!
Can you help me on this?

Thanks!

 
Reply With Quote
 
SGershon
Guest
Posts: n/a
 
      03-15-2005
Hi.

Thanks to everybody for the concerns.

I am using a date-picker to enter the date at the textboxes. Textboxes
are disabled to prevent direct editting.

> Finally, never trust user-entered dates and always check the
> content at the server.


I am checking the content by javascript at the client.
But now I am thinking (because you wrote "at the server"), that if the
user does not have javascript enabled I do not know what I am receiving
at the server... Is that why you said to check it at the server?
Also, if the user has JS disabled... he wont get the date-picker. Now
that I realised this my solution is to have the "disabled" text written
by a javacript document.write, cause I think this way user without JS
will have the textbox enabled. Is that right?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Manipulation: Week_Number back to Date wls Perl Misc 3 02-02-2005 06:48 AM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments