Wednesday, April 7, 2010

some javascript input field checking

function flatnoValidation(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;

if (unicode== 32)
{
return false; //if the key is the backspace key (which we should not allow)
}
else if (unicode== 96)
{
return false; //if the key is the backspace key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the backspace key (which we should not allow)
}
else
{
return true;
}

}

function blockUsedSymbols(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 96)
{
return false; //if the key is the ` key (which we should not allow)
}
else if (unicode== 39)
{
return false; //if the key is the ' key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the ~ key (which we should not allow)
}
else
{
return true;
}
}
function nameonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;

if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if(unicode==32)
{
return true;//if the key is the space key (which we should allow)

}
else if(unicode<65||unicode>90&&unicode<97||unicode>122)
{
return false; //if not a letter
}
return true;
}

function floorNoValidation(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if (unicode== 32)
{
return false;//if the key is the space key (which we should not allow)
}
else if(unicode<48||unicode>57)
{
return false;
}
return true;
}

function numberonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if (unicode== 32)
{
return true;//if the key is the space key (which we should allow)
}
else if(unicode<48||unicode>57)
{
return false;
}
return true;
}
function passwordonly(e)
{
var unicode = e.charCode? e.charCode : e.keyCode;
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else if(unicode==95)
{
return true;//if the key is the _ underscore key (which we should allow)
}
else if (unicode==32)
{
return false;//if the key is the space key (which we should not allow)
}
else if(unicode<48||unicode>57&&unicode<65||unicode>90&&unicode<97||unicode>122)
{
return false;
}
return true;
}

function trimThis(value1)
{
value1= value1.replace(/^\s+|\s+$/, '');
return value1;
}

function emailcheck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){

return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

return false
}

if (str.indexOf(at,(lat+1))!=-1){

return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

return false
}

if (str.indexOf(dot,(lat+2))==-1){

return false
}

if (str.indexOf(" ")!=-1){

return false
}

return true
}

function validateTextAreaLen(e,txt,len)
{
var unicode = e.charCode? e.charCode : e.keyCode;
var addrs = txt.value;
if(addrs.toString().length >= len)
{
if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}
else if (unicode== 46)
{
return true;//if the key is the delete key (which we should allow)
}
else
{
return false;
}
}
else
{
if (unicode== 96)
{
return false; //if the key is the ` key (which we should not allow)
}
else if (unicode== 39)
{
return false; //if the key is the ' key (which we should not allow)
}
else if (unicode== 126)
{
return false; //if the key is the ~ key (which we should not allow)
}
else
{
return true;
}

}
}

function onblurMonyonly(txtf)
{
if(trimThis(txtf.value).toString().length == 0)
{
txtf.value='0.00';
}
else if(txtf.value == '.')
{
txtf.value='0.00';
}
else if(txtf.value.toString().indexOf('.') != -1)
{
if(txtf.value.toString().indexOf('.') == txtf.value.toString().length-1)
{
txtf.value = txtf.value.toString()+'00';
}
else if(txtf.value.toString().indexOf('.') == txtf.value.toString().length-2)
{
txtf.value = txtf.value.toString()+'0';
}
}
else
{
txtf.value = txtf.value.toString()+'.00';
}

}

function resetMoneyOnly(txtf)
{
txtf.value = '';
}
function moneyonly(e,txtf)
{
var unicode=e.charCode? e.charCode : e.keyCode;
var st = txtf.value;
//alert(unicode)
if (unicode== 8)
{
return true;
//if the key isn't the backspace key (which we should allow)
}
else if (unicode== 8)
{
return true; //if the key is the backspace key (which we should allow)
}
else if (unicode== 9)
{
return true;//if the key is the tab key (which we should allow)
}

else if(unicode == 46)
{
if(st.toString().indexOf(".",0) != -1)
{
return false;
}

}
else if (unicode<48||unicode>57) //if not a number
{

return false //disable key press
}
else if(st.toString().indexOf(".",0) != -1)
{
if(st.toString().length - st.toString().indexOf(".",0) > 2)
{
return false;
}
}

return true;
}

No comments:

Post a Comment