Thursday, October 1, 2009

Numeric Validation in Javascript

/*
NUMERIC
prefix --> For number of digits before .(Period)
If you didn't give the period also it allows only the digits that given in the prefix part

suffix --> For the number of digits after the .(Period)

NumberOnly
If we specify suffix as 0 it allows only integer
*/

  
function NumericValidation(ctrlName, e, prefix, suffix) {

var evt = e || window.event;
var code = e.charCode ? e.charCode : e.keyCode;
var EndKey = 35;
var HomeKey = 36;
var NumeriPeriodKey = 110;
var PeriodKey = 190;
if (e.shiftKey || e.ctrlKey) {
return false;
}


//debugger;
// IE
if (evt.type == 'keydown') {

if ((code == EndKey || code == HomeKey || code == 37 || code == 38 || code == 39 || code == 40 || code == 9)) {
return true;
}

if (code == 46 || code == 8) {
var val = ctrlName.value;
var pos = val.indexOf('.');
var lenofBox = val.length;

if (code == 8) {
//Since back key deletes backward
if (doGetCaretPosition(ctrlName) == (pos + 1)) {

ctrlName.value = val.substring(0, pos)
}
else {
return true;
}
}
else (code == 46)
{
//Since del key deletes forward
if (doGetCaretPosition(ctrlName) == pos) {

ctrlName.value = val.substring(0, pos)
}
else {
return true;
}
}
}

if ((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode >= 96 && evt.keyCode <= 105) || (code == PeriodKey || code == NumeriPeriodKey)) {

var val = ctrlName.value;

if (val != "") {
var pos = val.indexOf('.');
var lenofBox = val.length;

//To allow only int
if ((suffix == 0) && (code == PeriodKey || code == NumeriPeriodKey)) {
return false;
}

//if already period is available
if ((code == PeriodKey || code == NumeriPeriodKey) && (pos > -1)) {
return false;
}

//if prefix reaches and the current one is not period the stop the event
if ((pos == -1 && lenofBox == prefix) && (code != PeriodKey && code != NumeriPeriodKey)) {
return false;
}

if (doGetCaretPosition(ctrlName) > pos) {

if (pos > -1) {
var decSting = val.substring(pos + 1, lenofBox + 1);
// debugger;
//alert(doGetCaretPosition(document.getElementById('TextBox1')));
if (decSting.length == suffix) {
return false;
}
}
}
else if (pos >= prefix) {
return false;
}

}

// alert(document.getElementById('TextBox1').value + String.fromCharCode(evt.keyCode));
return true;
}
else {
// alert(document.getElementById('TextBox1').value);
return false;
}
}
else if (evt.type == 'keypress') {

if ((code == 37 || code == 38 || code == 39 || code == 40) || (code == 46 || code == 8 || code == 9)) {
return true;
}

if ((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode >= 96 && evt.keyCode <= 105) || (code == PeriodKey || code == NumeriPeriodKey)) {

var val = ctrlName.value;

if (val != "") {
var pos = val.indexOf('.');
var lenofBox = val.length;

//To allow only int
if ((suffix == 0) && (code == PeriodKey || code == NumeriPeriodKey)) {
return false;
}

//if already period is available
if ((code == PeriodKey || code == NumeriPeriodKey) && (pos > -1)) {
return false;
}

//if prefix reaches and the current one is not period the stop the event
if ((pos == -1 && lenofBox == prefix) && (code != PeriodKey || code != NumeriPeriodKey)) {
return false;
}

if (doGetCaretPosition(ctrlName) > pos) {

if (pos > -1) {
var decSting = val.substring(pos + 1, lenofBox + 1);
// debugger;
//alert(doGetCaretPosition(document.getElementById('TextBox1')));
if (decSting.length == suffix) {
return false;
}
}
}
else if (pos >= prefix) {
return false;
}
}

// alert(document.getElementById('TextBox1').value + String.fromCharCode(evt.keyCode));
return true;
}
else {
// alert(document.getElementById('TextBox1').value);
return false;
}
}
}

function doGetCaretPosition(ctrl) {
var CaretPos = 0; // IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange();
Sel.moveStart('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart == '0') CaretPos = ctrl.selectionStart;
return (CaretPos);
}

No comments: