Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting user input 2

Status
Not open for further replies.

usagimd

Programmer
Jul 6, 2011
2
BR
Hello everyone,

I'm new here, this is my first post. After a search on google and this forum I couldn' find what I'm looking for.

I was searching about conversions "hh:mm" to seconds and vice-versa.

In my situation I have only one field on the DB. Called duration_in_seconds

But I don't want my users to calculate theyr seconds so I have 2 fields:

Starting Hour
Finishing Hour

This information is sent to the controller and it's model calculates the seconds.

Still I need to SHOW to the user the result in seconds in another field (not inputable) just to show them how many seconds they've spent on a task.

I know: Why the hell users need to see this?

But you guys know, sometimes they just want it and nothing will take it of theyr heads.

Here is something I found in this forum:
Code:
function timeSpanToInteger(tmspan) {
    var fieldArray = tmspan.split(":");
    return parseint(fieldArray[0])*60 + parseint(fieldArray[1]);
}
It wasn't exactly like this I've change to fit my needs.

The original code was posted by Diancecht while helping DoctorGonzo with timespan -> minutes

About my problem:

I found also something about using input:focus but it seems to not work with IE, any idea of a cross-browser solution?

Thank you guys. And if someone wants to know about the project I'm working: redmine.org

I'm learning JS CSS and Ruby/Ruby on Rails and trying to find something to contribute with redmine, I'm entering in the world of open source software :p

Cheers
 
Well you could have a button they can press to have it calculate the seconds after both are filled in, or simply use the onKeyPress events of both textboxes to initiate the calculation.

You'll need to have some error checking so you don't attempt to perform the conversions on empty fields or none compliant fields.

Code:
<input type="text" ... onkeypress="calculateFunction();">




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Hi
[ul]
[li]JavaScript is case sensitive, the function is [tt]parse[red]I[/red]nt()[/tt]. As the hour and minutes used to be padded with 0 and 0 is the prefix to indicate octal, better specify the base too : [tt]parseInt[teal]([/teal]fieldArray[teal][[/teal][purple]0[/purple][teal]][/teal][highlight][teal],[/teal][purple]10[/purple][/highlight][teal])*[/teal][purple]60[/purple] [teal]+[/teal] parseInt[teal]([/teal]fieldArray[teal][[/teal][purple]1[/purple][teal]][/teal][highlight][teal],[/teal][purple]10[/purple][/highlight][teal]);[/teal]
[/tt] .[/li]
[li][tt]input:focus[/tt] sounds like a CSS rule. Indeed, Explorer had problem with the [tt]:focus[/tt] pseudo-class, but, as far as I know, on [tt]a[/tt] not on [tt]input[/tt]. But that not affects JavaScript anyway.[/li]
[/ul]


Feherke.
 
Thanks guys,

I'm beginner with JS, but there is a lot of things I want to do "onKeyPress" like validations and other actions (like inserting ":" automatically cause on Ruby on Rails forums ppl told me that javascript is better.

Also I've set up programming validations at the model and controller. So if it's something wrong the user will be redirected to the same page with messages indicating the errors on each field.

Thank you guys, I'm gonna keep practicing JS here.

Cheers and happ coding.
Matheus Dewes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top