Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: JavaScript, Created At: 10/24/2016 8:07:48 PMViews:

HTML view:
Copy Source | Copy HTML
  1. window.onload=function(){
  2.     var tst1=id("tst1"),
  3.         dbg1=id("dbg1"),
  4.         tst2=id("tst2"),
  5.         dbg2=id("dbg2");
  6.     event(tst1,"keypress",function(e){
  7.         var nonPrintable,printable; // параметры с помощью которых попытаемся решить проблему с кросс браузерностью
  8.         if((e.charCode=== 0||e.charCode===undefined)&&e.which=== 0)
  9.             nonPrintable=e.keyCode;
  10.         printable=e.which?e.which:(e.which===undefined&&e.charCode===undefined)?e.keyCode:undefined;
  11.         dbg1.innerHTML="keyCode: "+e.keyCode+", charCode: "+e.charCode+", which: "+e.which+
  12.         " #### caf: "+printable+", yk: "+nonPrintable;
  13.     });
  14.     event(tst2,"keydown",function(e){
  15.         dbg2.innerHTML="keyCode: "+e.keyCode+", charCode: "+e.charCode+", which: "+e.which;
  16.     });
  17.     var num1=id("num1");
  18.     // тегу input добавлем проверку ввода
  19.     event(num1,"keydown",function(e){
  20.         e = (e)?e: (window.event)?window.event:undefined;
  21.         var target;
  22.         if(e) {
  23.             target = (e.target)?e.target:e.srcElement;
  24.         }
  25.         console.dir(e);
  26.         if (e.keyCode===13){// Enter if need
  27.             e.preventDefault? e.preventDefault() : e.returnValue = false;
  28.         }
  29.         if ((e.keyCode>47&&e.keyCode<58)||e.keyCode===8||e.keyCode===37||e.keyCode===39||e.keyCode==46){
  30.             e.returnValue=true; // [0..9] -> [47..58], 8 - backsace, 37 - Left, 39 - Right, 46 -Delete
  31.         }else if (e.keyCode===188 || e.keyCode===190){
  32.             var s=target.value, // input field!
  33.                     i= 0,count= 0;
  34.             while (s[i]){
  35.                 if (s[i]==='.' || s[i]===',')
  36.                     count++;
  37.                 i++;
  38.             }
  39.             e.returnValue=count=== 0;// Точка или запятая может быть только одна в водимом числе
  40.             if(count!== 0 && e.preventDefault)// Condition for FireFox
  41.                 e.preventDefault();
  42.         }else{
  43.             e.preventDefault? e.preventDefault() : e.returnValue = false;
  44.         }
  45.     });
  46. };
  47. function id(s){
  48.     return document.getElementById(s);
  49. }
  50. function event(obj,name,handler){
  51.     if (obj.attachEvent){
  52.         obj.attachEvent("on"+name,handler);
  53.     }else{
  54.         obj.addEventListener(name,handler);
  55.     }
  56. }

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017