Filter Keys jQuery Plugin

posted by sacah on javascript, programming, sacah,

To help users fill in a form, you can filter out keys you don’t want them to try and use. If it’s a numeric field, supply the data-filterkeys=’[0-9]’ and they can only enter numbers. If it’s a price, use data-filterkeys=’[0-9$.]’ and they can enter numbers and $ and .

I have written it to also use a class of .filterkeys on each input, rather than just searching for inputs with a custom attribute, simply because the class search is faster. If you really don’t want to have a filterkey class on all inputs requiring the filterkeys functionality, it’s an easy change:

$(‘.filterkeys’, this)

to

$(‘[data-filterkeys]’, this)

Download from BitBucket