Home   Best Sellers   Blogging   Coding & Design   Technology   SEO   Travel & living   Career   Videos   Tips   Calculators     
Home  »     »  JS Function to check whether the string is numeric

JS Function to check whether the string is numeric

Tuesday, March 1, 2011

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);">





How to link to this page?
If you wish to link to this page from your website, simply Copy and paste the above HTML code to your web page. It will appear on your page as:
JS Function to check whether the string is numeric.




Share this!




comments powered by Disqus

This Weeks 7 Popular Posts


Subscribe to Recent Posts by Email
Stay connected to CROZOOM with regular Email notices of new Techie articles and IT Jobs. Updates will be delivered to your Inbox as soon as they are posted online.

Enter Your Email Address:  

Delivered by FeedBurner   RSS Feed

Search this Blog   



Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to recruit.vo@gmail.com   Read more »