Posted By

Leech on 07/21/06


Tagged

class input forms restrict


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jkochis


Restrict v1.0


 / Published in: JavaScript
 

URL: http://jsfromhell.com/forms/restrict

Class to mask inputs and restrict characters. Created: 2005.10.08

  1. /*
  2. **************************************
  3. * Restrict Class v1.0 *
  4. * Autor: Carlos R. L. Rodrigues *
  5. **************************************
  6. */
  7.  
  8. //========================================================
  9. // REQUIRES http://www.jsfromhell.com/geral/event-listener
  10. //========================================================
  11.  
  12. Restrict = function(form){
  13. this.form = form, this.field = {}, this.mask = {};
  14. }
  15. Restrict.field = Restrict.inst = Restrict.c = null;
  16. Restrict.prototype.start = function(){
  17. var $, __ = document.forms[this.form], s, x, j, c, sp, o = this, l;
  18. var p = {".":/./, w:/\w/, W:/\W/, d:/\d/, D:/\D/, s:/\s/, a:/[\xc0-\xff]/, A:/[^\xc0-\xff]/};
  19. for(var _ in $ = this.field)
  20. if(/text|textarea|password/i.test(__[_].type)){
  21. x = $[_].split(""), c = j = 0, sp, s = [[],[]];
  22. for(var i = 0, l = x.length; i < l; i++)
  23. if(x[i] == "\\" || sp){
  24. if(sp = !sp) continue;
  25. s[j][c++] = p[x[i]] || x[i];
  26. }
  27. else if(x[i] == "^") c = (j = 1) - 1;
  28. else s[j][c++] = x[i];
  29. o.mask[__[_].name] && (__[_].maxLength = o.mask[__[_].name].length);
  30. __[_].pt = s, addEvent(__[_], "keydown", function(e){
  31. var r = Restrict.field = e.target;
  32. if(!o.mask[r.name]) return;
  33. r.l = r.value.length, Restrict.inst = o; Restrict.c = e.key;
  34. setTimeout(o.onchanged, r.e = 1);
  35. });
  36. addEvent(__[_], "keyup", function(e){
  37. (Restrict.field = e.target).e = 0;
  38. });
  39. addEvent(__[_], "keypress", function(e){
  40. o.restrict(e) || e.preventDefault();
  41. var r = Restrict.field = e.target;
  42. if(!o.mask[r.name]) return;
  43. if(!r.e){
  44. r.l = r.value.length, Restrict.inst = o, Restrict.c = e.key || 0;
  45. setTimeout(o.onchanged, 1);
  46. }
  47. });
  48. }
  49. }
  50. Restrict.prototype.restrict = function(e){
  51. var o, c = e.key, n = (o = e.target).name, r;
  52. var has = function(c, r){
  53. for(var i = r.length; i--;)
  54. if((r[i] instanceof RegExp && r[i].test(c)) || r[i] == c) return true;
  55. return false;
  56. }
  57. var inRange = function(c){
  58. return has(c, o.pt[0]) && !has(c, o.pt[1]);
  59. }
  60. return (c < 30 || inRange(String.fromCharCode(c))) ?
  61. (this.onKeyAccept && this.onKeyAccept(o, c), !0) :
  62. (this.onKeyRefuse && this.onKeyRefuse(o, c), !1);
  63. }
  64. Restrict.prototype.onchanged = function(){
  65. var ob = Restrict, si, moz = false, o = ob.field, t, lt = (t = o.value).length, m = ob.inst.mask[o.name];
  66. if(o.l == o.value.length) return;
  67. if(si = o.selectionStart) moz = true;
  68. else if(o.createTextRange){
  69. var obj = document.selection.createRange(), r = o.createTextRange();
  70. if(!r.setEndPoint) return false;
  71. r.setEndPoint("EndToStart", obj); si = r.text.length;
  72. }
  73. else return false;
  74. for(var i in m = m.split(""))
  75. if(m[i] != "#")
  76. t = t.replace(m[i] == "\\" ? m[++i] : m[i], "");
  77. var j = 0, h = "", l = m.length, ini = si == 1, t = t.split("");
  78. for(i = 0; i < l; i++)
  79. if(m[i] != "#"){
  80. if(m[i] == "\\" && (h += m[++i])) continue;
  81. h += m[i], i + 1 == l && (t[j - 1] += h, h = "");
  82. }
  83. else{
  84. if(!t[j] && !(h = "")) break;
  85. (t[j] = h + t[j++]) && (h = "");
  86. }
  87. o.value = o.maxLength > -1 && o.maxLength < (t = t.join("")).length ? t.slice(0, o.maxLength) : t;
  88. if(ob.c && ob.c != 46 && ob.c != 8){
  89. if(si != lt){
  90. while(m[si] != "#" && m[si]) si++;
  91. ini && m[0] != "#" && si++;
  92. }
  93. else si = o.value.length;
  94. }
  95. !moz ? (obj.move("character", si), obj.select()) : o.setSelectionRange(si, si);
  96. }

Report this snippet  

You need to login to post a comment.