Thursday, September 4, 2008

How to remove alphabets,special characters and white space in a string and return only numbers using javascript?

<script language="javascript" type="text/javascript">

function RemoveSpecialChar()
{
var A;
var B;
A=document.getElementById('TextBox1').value;
B= filterNum(A);
document.getElementById('TextBox2').value=noAlpha(whitespace(B));
function filterNum(str)
{
re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
return str.replace(re, "");
}
function whitespace(str)
{
str = str.replace(/\s+/g,'');
return str;
// \s+ Description
// + 1 or more of previous expression.
// \s Matches any white-space character.
// Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}].
// If ECMAScript-compliant behavior is specified with the ECMAScript option,
// \s is equivalent to [ \f\n\r\t\v].
}
function noAlpha(obj)
{
reg = /[^0-9.,]/g;
return obj.replace(reg,"");
}
}
</script>

No comments:

 
Feedback Form