-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnumericInput.min.js
2 lines (2 loc) · 1.35 KB
/
numericInput.min.js
1
2
// Author: Joshua De Leon - File: numericInput.js - Description: Allows only numeric input in an element. - If you happen upon this code, enjoy it, learn from it, and if possible please credit me: www.transtatic.com
(function(e){function n(e){if(e.selectionStart)return e.selectionStart;else if(document.selection){e.focus();var t=document.selection.createRange();if(t==null)return 0;var n=e.createTextRange(),r=n.duplicate();n.moveToBookmark(t.getBookmark());r.setEndPoint("EndToStart",n);return r.text.length}return 0}var t={allowFloat:false,allowNegative:false,min:undefined,max:undefined};e.fn.numericInput=function(r){var i=e.extend({},t,r);var s=i.allowFloat;var o=i.allowNegative;var u=i.min;var a=i.max;if(u==a){throw"The minimum value cannot be the same as the max value"}else if(u>a){var f=u;u=a;a=f}this.keypress(function(t){var r=t.which;var i=e(this).val();if(r>0&&(r<48||r>57)){if(s==true&&r==46){if(o==true&&n(this)==0&&i.charAt(0)=="-")return false;if(i.match(/[.]/))return false}else if(o==true&&r==45){if(i.charAt(0)=="-")return false;if(n(this)!=0)return false}else if(r==8)return true;else return false}else if(r>0&&r>=48&&r<=57){if(o==true&&i.charAt(0)=="-"&&n(this)==0)return false}});this.blur(function(t){var n=e(this).val();if(n.length>0){var r=parseFloat(n);if(u!==undefined&&r<u){e(this).val(u)}if(a!==undefined&&r>a){e(this).val(a)}}});return this};})(jQuery)