Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove letters, symbols except numbers

How can you remove letters, symbols such as ∞§¶•ªºº«≥≤÷ but leaving plain numbers 0-9, I want to be able to not allow letters or certain symbols in an input field but to leave numbers only.

Demo.

If you put any symbols like ¡ € # ¢ ∞ § ¶ • ª or else, it still does not remove it from the input field. How do you remove symbols too? The \w modifier does not work either.

like image 268
MacMac Avatar asked Jul 11 '11 11:07

MacMac


People also ask

How do I remove all characters from a string except numbers?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

How do I remove a specific character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

Can regex replace characters?

RegEx makes replace ing strings in JavaScript more effective, powerful, and fun. You're not only restricted to exact characters but patterns and multiple replacements at once.


1 Answers

You can use \D which means non digits.

var removedText = self.val().replace(/\D+/g, ''); 

jsFiddle.

You could also use the HTML5 number input.

<input type="number" name="digit" /> 

jsFiddle.

like image 117
alex Avatar answered Oct 09 '22 04:10

alex