How do I strip any characters out of the telephone

2019-06-02 18:43发布

问题:

How do I strip any characters that are not a number out of the telephone field, including spaces. I need to have a string of numbers 0-9. No () or spaces.

i have a maskedinput for telephone (999) 999-9999

Phone Number:         
<input id="phone" type="text" />   (999) 999-9999 

i just need 9999999999

i am using maskedinput plugin http://digitalbush.com/projects/masked-input-plugin/

回答1:

var str = "(999) 999-9999";

str = str.replace(/\D/g,'');

Removes anything that is not a digit \D.



回答2:

http://jsfiddle.net/tylermwashburn/YsHjR/

var phonenum = "(123) 456-7890",
    notnum = /\D/g;

phonenum = phonenum.replace(notnum, '');

alert(phonenum);

This should form it nicely.