views:

41

answers:

2

I am using jquery-keyfilter plugin to mask textbox inputs. However the textboxes are added to the page dynamically and I cannot figure out how to apply the key filter to them.

I have tried

$('#myelement').live('keyfilter', myFunction );

Also

$('#myelement').live('keyfilter', /regex/);

Kai: comment helps, but it seems my problem is still not solved

I want a keyfilter like this

(/^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$/);

that will only accept currency/money values but it seems like jquery-keyfilter does not work with this regex. Is this something I am doing wrong or should I look at using another plugin or just code it myself?

+3  A: 

"keyfilter" is not an event and you can NOT use live().
According to API of the plugin, it should be

$('#myelement').keyfilter(function(c) { return c != 'a'; }); 

$('#myelement').keyfilter(/[\dA-F]/); 
Kai
Thank you I did not realise you can only use live for events
Daveo
A: 

Below solution works for non-first character

  
$("#myelement").live("keypress", function(){  
  $(this).keyfilter((/^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$/);  
});

Below solution works for input field already clicked

  
$("#myelement").live("click", function(){    
  $(this).keyfilter((/^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$/);  
});  
fuss
This does not work see herehttp://jsbin.com/esezi/edit it will not allow me to enter a $ sign by itself or a comma test it here yourself http://regexr.com?2rqsuI have it working now for key entry using /[\d\$\,\.]/ and I just use jQuery validation plugin with the final regex for when the field loses focus. Sure I could write a regex to do exactly what I want on keypress but eh. It would be too long and nasty to maintain
Daveo