Javascript function to check the string in the textbox is numeric
function name : isNumber
parameters : Input control object and flag to allow decimal points
Function doesn't allow non numeric characters in the control
the function can be called on onKeyUp event of the control.
if permitDecimal = 0 then decimal point '.' is not allowed in string, else dot is allowed.
function isNumber( obj ,permitDecimal ){
var val = document.getElementById(obj).value ;
var te=val.match(/\d+\.?\d*/);
document.getElementById(obj).value=te;
val=te;
if(val!=""){
if( isNaN(val) ){
endOFString=val.length-1;
val=val.substring(0,endOFString);
}
if(!permitDecimal){
if(val){
val=val.toString();
document.getElementById(obj).value=val.replace(/\./g,'');
}
}else{
document.getElementById(obj).value=val;
}
}
}
Sample usage
<input id="txtpersontel" type="text" name="persontel" maxlength="20" style="width:125px"
onkeyUP=" isNumber('txtpersontel',0);">
Tweet
function name : isNumber
parameters : Input control object and flag to allow decimal points
Function doesn't allow non numeric characters in the control
the function can be called on onKeyUp event of the control.
if permitDecimal = 0 then decimal point '.' is not allowed in string, else dot is allowed.
function isNumber( obj ,permitDecimal ){
var val = document.getElementById(obj).value ;
var te=val.match(/\d+\.?\d*/);
document.getElementById(obj).value=te;
val=te;
if(val!=""){
if( isNaN(val) ){
endOFString=val.length-1;
val=val.substring(0,endOFString);
}
if(!permitDecimal){
if(val){
val=val.toString();
document.getElementById(obj).value=val.replace(/\./g,'');
}
}else{
document.getElementById(obj).value=val;
}
}
}
Sample usage
<input id="txtpersontel" type="text" name="persontel" maxlength="20" style="width:125px"
onkeyUP=" isNumber('txtpersontel',0);">
Tweet
Comments
Post a Comment