Regex to remove letters, symbols except numbers
Asked Answered
C

6

85

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.

Cosby answered 11/7, 2011 at 11:20 Comment(1)
\w is not a modifier but a shortcut of [A-Za-z0-9_] or word characters.Anthracene
A
144

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.

Anthracene answered 11/7, 2011 at 11:23 Comment(6)
Probably want to replace more than just the first non-digit found -- change regexp to /\D+/Oresund
shouldn't it be /\D+/g ?Lectern
/\D+/g has the same effect as to /\D/g.Horripilation
I find the fiddle for number input unuseful: a) it includes the javascript for removing non-numbers and b) if the above js is removed, it does nothing (for me) for the purpose of removing non-numbersBilberry
@Bilberry You still need to validate it yourself. It does give you the arrows for incrementing/decrementing the number though.Anthracene
.keydown() is not enough if user is allowed to paste text into field.Culley
R
43

Use /[^0-9.,]+/ if you want floats.

Rapt answered 11/4, 2012 at 18:39 Comment(5)
If you want a string that could contain a float value ;)Anthracene
what is that "," for?Caraway
@sbk in some regions they use the comma instead of the dot to mark decimal places :)Rapt
also negatives /[^-0-9.,]+/Serpigo
@KlemenTusar I tried this one with "245k" and didn't work..Hagler
E
22

Simple:

var removedText = self.val().replace(/[^0-9]+/, '');

^ - means NOT

Esquimau answered 11/7, 2011 at 11:22 Comment(1)
var removedText = self.val().replace(/[^0-9]+/g, ''); to keep all numbers. albeit 8.10+3 would give 8103... Not sure that is what's asked for in the question.Curnin
R
8

Try the following regex:

var removedText = self.val().replace(/[^0-9]/, '');

This will match every character that is not (^) in the interval 0-9.

Demo.

Riendeau answered 11/7, 2011 at 11:21 Comment(1)
I hate being 32 seconds late :DEsquimau
H
4

If you want to keep only numbers then use /[^0-9]+/ instead of /[^a-zA-Z]+/

Hugh answered 11/7, 2011 at 11:23 Comment(0)
A
0

Excluding special characters:

/^[^@~!@#$%^&()_=+\';:"/?>.<,-]$/`

This regular expression helps to exclude special characters from the input.

Exclude special characters and emojis:

/^([^\u2700-\u27BF\uE000-\uF8FF\uDD10-\uDDFF\u2011-\u26FF\uDC00-\uDFFF\uDC00-\uDFFF\u005D\u007C@~!@#$%^&()_=+[{}"\';:"/?>.<,-\s])$/`

This is a regular expression to exclude both special characters and emojis from the input. Given are the Unicode ranges of the emojis, mathematical symbols, and symbols in other languages.

Adjudicate answered 27/10, 2022 at 13:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.