How to format dates and telephones in form_for?

Well, that’s basically it, how can I make an input field for date like
mm/dd/yyyy and for telephone like +xx (xx) xxxx-xxxx.

Thank you,
Rodrigo

make on javascript for that and call in :onkeypress => ‘yourJS(this);’ i
think so
Em 30/06/2011, s 22:23, Rodrigo R. escreveu:

Rodrigo R. wrote in post #1008546:

Well, that’s basically it, how can I make an input field for date like
mm/dd/yyyy and for telephone like +xx (xx) xxxx-xxxx.

jQuery UI has a pretty nice date picker:

As for phone numbers ActionView has some nice number helpers, including
one for phone numbers:

Can’t I do that only with ruby and rails?

On Fri, Jul 1, 2011 at 3:25 AM, Leoncio C.

On Jul 1, 2011, at 9:13 AM, Rodrigo R. wrote:

Can’t I do that only with ruby and rails?

On Fri, Jul 1, 2011 at 3:25 AM, Leoncio C. <[email protected]

wrote:
make on javascript for that and call in :onkeypress =>
‘yourJS(this);’ i think so
Em 30/06/2011, s 22:23, Rodrigo R. escreveu:

You could certainly clean up any user input in a before_validation
filter method. I don’t know how you would figure out which mask to
use, but what I usually do is first strip out anything that isn’t a
numeral, and then take the numbers back through a mask so that first n
numbers are area code, next are exchange, then station, then extension
if anything is left over. If you had multiple nations or any optional
elements (country code) then the problem gets a lot harder to solve.

By the way, as Leoncio pointed out, this could all be done in
JavaScript, and the implementation is largely the same; string
operations being more or less identical (and with Prototype.js, even
having the same names for the functions) in either JavaScript or Ruby.

Walter

Hai!

<%= f.date_select :birthday,
{:start_year => Time.now.year,
:end_year => 1900,
:use_short_month => true,
:order => [:month, :day, :year],
:prompt => {:month => ‘Month’, :day => ‘Day’, :year => ‘Year’}},
{:class => ‘year’,
:id => ‘user_birthday’}
%>

validates_presence_of :birthday, :message => ‘is a required field’

All the best :slight_smile:

That’s done with JavaScript. Have a google for the term ‘input mask’,
and step well back when you see the zillions of results.

Walter

How can I manage to make it appear automatically, the + and () and - in
the
telephone +00(00)0000-0000 and the / in the date like mm/dd/yyyy, as the
user types only numbers? And how to enable only numbers to be typed?

And what type are those kind of information saved in the database?
strings?

Dude, one lil example:

put this in your public/javascript

function maskTelefone(source, event) {
return mask(source, event, ‘99 9999-9999’, ‘0123456789’);
}

then, in your view, call like this

<%= f.text_field :del_fone, :size => 13, :maxlength => 12, :onkeypress
=>
“return maskTelefone(this,event);” %>

works fine fella!

any doubt, please, talk

2011/7/1 Walter Lee D. [email protected]

yep, no problem for that

2011/7/1 Rodrigo R. [email protected]

Thank you, I didn’t quite understand cause I haven’t study javascript
yet,
but I think I got the idea.

Just one more thing, can I save all that just as strings in the
database?

On Fri, Jul 1, 2011 at 4:15 PM, leoncio caminha

Also what about the date and datetime types? How do I use them and is it
better to use one of those instead of a string?

On Fri, Jul 1, 2011 at 4:27 PM, leoncio caminha

yep, much better keep just the numbers into database, better for search
and
cast (if necessary), dont save with "-"or “XX” and the other stuffs and
occuped less space on DB

2011/7/1 Rodrigo R. [email protected]

I was talking to my group here and they said it’s more interesting to
save
telephone with numbers, is it ok if I save as integer and still appear
the
+, () and - automatically at the page?

On Fri, Jul 1, 2011 at 4:27 PM, leoncio caminha

I would use Date, Datetime or Timestamp data types, depend on the
problem
you need to model, in order to model dates (and/or dates and times).
If you are already using jQuery you could use the jQuery UI datepicker (
Datepicker | jQuery UI). You can configure the layout and
many other options.

Hope it helps.