Regex That Accepts Only Numbers (0-9) and No Characters

Regex that accepts only numbers (0-9) and NO characters

Your regex ^[0-9] matches anything beginning with a digit, including strings like "1A". To avoid a partial match, append a $ to the end:

^[0-9]*$

This accepts any number of digits, including none. To accept one or more digits, change the * to +. To accept exactly one digit, just remove the *.

UPDATE: You mixed up the arguments to IsMatch. The pattern should be the second argument, not the first:

if (!System.Text.RegularExpressions.Regex.IsMatch(textbox.Text, "^[0-9]*$"))

CAUTION: In JavaScript, \d is equivalent to [0-9], but in .NET, \d by default matches any Unicode decimal digit, including exotic fare like ႒ (Myanmar 2) and ߉ (N'Ko 9). Unless your app is prepared to deal with these characters, stick with [0-9] (or supply the RegexOptions.ECMAScript flag).

Regex for numbers only

Use the beginning and end anchors.

Regex regex = new Regex(@"^\d$");

Use "^\d+$" if you need to match more than one digit.


Note that "\d" will match [0-9] and other digit characters like the Eastern Arabic numerals ٠١٢٣٤٥٦٧٨٩. Use "^[0-9]+$" to restrict matches to just the Arabic numerals 0 - 9.


If you need to include any numeric representations other than just digits (like decimal values for starters), then see @tchrist's comprehensive guide to parsing numbers with regular expressions.

RegEx to accept only numbers between 0-9

This works

^0\d{9,10}$

Matches 0, then 9 to 10 digits.

You could also use Regex.IsMatch instead:

Regex telephoneExp = new Regex(@"^0\d{9,10}$");
if (!telephoneExp.IsMatch(txtTelephoneNumber.Text))
{
MessageBox.Show("The telephone number is not valid, it must only contain digits (0-9) and be either 10 or 11 digits in length.");
return false;
}

REGEX a-z 0-9 but not only numbers

The \D shorthand class means any non-digit symbol. You should remove it from the pattern (so that it becomes "^[A-Za-z0-9_-]{10,30}$") for the matches to return true, as 1 is a digit in 1Regex that accepts only numbers (0-9) and NO characters Regex for numbers only RegEx to accept only numbers between 0-9 REGEX a-z 0-9 but not only numbers Regex to alloe333e.

If you want to place a restriction (the string cannot consist of only digits) use an anchored look-ahead:

^(?![0-9]+$)[A-Za-z0-9_-]{10,30}$

Here is a demo

Or, a shortened version with i modifier making the pattern case-insensitive:

(?i)^(?![0-9]+$)[A-Z0-9_-]{10,30}$

Regex to allow only numbers and max 2 digits

I would recommend to update regex to allow 0 or 2 digits- /^[0-9]{0,2}$/.

The following values will match: '', '0', '1', ... '10', ... '99'.

keypress event has the following fields:

  • event.target.value - value of input before new key was pressed
  • event.key - string value of the key that was pressed

When event.target.value and event.key are combined together we can estimate new value of the input.

event.preventDefault() call can be used to block keypress event if the resulting value does not match the needed pattern.

document.getElementById('myinput').addEventListener('keypress', event => {
if (!`${event.target.value}${event.key}`.match(/^[0-9]{0,2}$/)) {
// block the input if result does not match
event.preventDefault();
event.stopPropagation();
return false;
}
});
<input id="myinput" type="text" value="" />

numbers not allowed (0-9) - Regex Expression in javascript

Simply:

/^([^0-9]*)$/

That pattern matches any number of characters that is not 0 through 9.

I recommend checking out http://regexpal.com/. It will let you easily test out a regex.



Related Topics



Leave a reply



Submit