I would go ahead and code the input type="number" element from HTML5, then 
provide a fallback for visitors whose browsers don't understand that element. 
Here's a Prototype.js solution, modify if needed to jQuery depending on your 
environment:

//include prototype.js before this point
document.observe('dom:loaded', function(){
  var test = new Element('input',{type:'number'});
  if(test.type != 'number'){
    $$('input[type="number"]').each(function(elm){
      elm.observe('blur',function(evt){
        this.setValue($F(this).gsub(/[^0-9]+/,'');
    });
  }
});

Note that if you have a lot of number fields on the same page, this will suck 
memory, because it defines a callback listener on each field separately. Take a 
look at the rails.js code for an example of how to force form fields to 
"bubble" (they don't, usually, not in most browsers) so that you can define a 
single listener function on the blur event and work out backward whether the 
field that threw that event needs this filter or not. That will scale much 
better than this technique, but it's more opaque to look at as an example.

Walter

On Dec 12, 2011, at 4:28 PM, Angelo Cordova wrote:

> Hello people
> 
> I need your help again.
> 
> What I want to do is make a text field accepts only numeric values...
> Thant's means if I press a letter or a simbol the text field should
> not be filled, it should be just if I press a number.
> 
> Is there a rails way (rails 3.0.9) to achieve this issue?
> 
> Thanks
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to