Tuesday, November 18, 2008

How to remove white space,special characters and return only numbers using 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,"");
}
}

No comments:

 
Feedback Form