Posted By

Leech on 07/21/06


Tagged

forms restrict mask


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jkochis
shachi


Masked Input v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/forms/masked-input

Class to mask inputs (does not work right on opera). Created: 2005.08.08 - Modified: 2005.11.19

  1. /**************************************
  2. * Jonas Raoni Soares Silva
  3. * http://www.joninhas.ath.cx
  4. **************************************/
  5.  
  6. //========================================================
  7. // REQUIRES http://www.jsfromhell.com/geral/event-listener
  8. //========================================================
  9.  
  10. MaskInput = function(f, m){ //v1.0
  11. function mask(e){
  12. var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[À-ÿ]/i, "8": /./ },
  13. rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
  14. function accept(c, rule){
  15. for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
  16. if(r & i && patterns[i].test(c))
  17. break;
  18. return i <= r || c == rule;
  19. }
  20. var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
  21. (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
  22. r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
  23. : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
  24. r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
  25. }
  26. for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
  27. addEvent(f, i, mask);
  28. };

Report this snippet  

You need to login to post a comment.