Get Numbers from String in Javascript andrey четверг, 3 апреля 2014 г. No Comment

In this post, We are going to learn how to get only numbers from string using Javascript. To do this we are going to use one of the power full weapon Regular Expression.

In this bellow example we are getting value from text box and get only numbers using javascript's .replace().replace() method.

Here we are going to use non-digit character equivalent ( \D ). Just find the non-digit character and replace with " " empty.

var $ = function (id) {
return document.getElementById(id);
};
$('btn').addEventListener('click', function () {
$('num').innerHTML = $('abc').value.replace(/\D/g, '');
});

NOTE : Here I used $ as a shorthand for document.getElementById(). To know more about shorthand, you can refer one of my previous post Shorthand for getElementById in Javascript.

Or you can simply use like this below, 

document.getElementById('btn').addEventListener('click', function() {
document.getElementById('num').innerHTML = document.getElementById('abc').value.replace(/\D/g, '');
});

after click on the "Click me !!!" button, it will alert the number you used in text box.
In this post, We are going to learn how to get only numbers from string using Javascript. To do this we are going to use one of the power full weapon Regular Expression.

In this bellow example we are getting value from text box and get only numbers using javascript's .replace().replace() method.

Here we are going to use non-digit character equivalent ( \D ). Just find the non-digit character and replace with " " empty.

var $ = function (id) {
return document.getElementById(id);
};
$('btn').addEventListener('click', function () {
$('num').innerHTML = $('abc').value.replace(/\D/g, '');
});

NOTE : Here I used $ as a shorthand for document.getElementById(). To know more about shorthand, you can refer one of my previous post Shorthand for getElementById in Javascript.

Or you can simply use like this below, 

document.getElementById('btn').addEventListener('click', function() {
document.getElementById('num').innerHTML = document.getElementById('abc').value.replace(/\D/g, '');
});

after click on the "Click me !!!" button, it will alert the number you used in text box.
by Jillur Rahman

Jillur Rahman is a Web designers. He enjoys to make blogger templates. He always try to make modern and 3D looking Templates. You can by his templates from Themeforest.

Follow him @ Twitter | Facebook | Google Plus

No Comment