I want to know what a regex would look like for:

- only whole numbers
- only numbers with less than or equal to two decimal places (23, 23.3, 23.43)

Whole numbers only

```
/\d+/
```

One or two decimal places:

```
/\d(\.\d{1,2})?/
```

Only whole numbers:

```
/^\d+$/
# explanation
\d match a digit
+ one or more times
```

Numbers with at most 2 decimal places:

```
/^\d+(?:\.\d{1,2})?$/
# explanation
\d match a digit...
+ one or more times
( begin group...
?: but do not capture anything
\. match literal dot
\d match a digit...
{1,2} one or two times
) end group
? make the entire group optional
```

Notes:

- The slashes denote start and end of pattern
`^`

and`$`

are start and end of string anchors. Without these, it will look for matches anywhere in the string. So`/\d+/`

matches`'398501'`

, but it also matches`'abc123'`

. The anchors ensures the*entire*string matches the given pattern.- If you want to allow negative numbers, add a
`-?`

before the first`\d`

. Again,`?`

denotes "zero or one time."

```
var rx = new RegExp(/^\d+(?:\.\d{1,2})?$/);
console.log(rx.test('abc')); // false
console.log(rx.test('309')); // true
console.log(rx.test('30.9')); // true
console.log(rx.test('30.85')); // true
console.log(rx.test('30.8573')); // false
```

I. `[1-9][0-9]*`

if the number should be greater than zero (any series of digits starting with a nonzero digit). if it should be zero or more: `(0|[1-9][0-9]*)`

(zero or a nonzero number). If it can be negative: `(0|-?[1-9][0-9]*)`

(zero or a nonzero number that can have a minus before it.)

II. a regex like I. followed by: `(\.[0-9]{1,2})?`

that means, optionally a dot followed by one or two digits.

Similar Questions

I want to make a javascript (or jquery) function that works onChange and allow only to write 3 numbers : 5, 15 or 25 If the user write an other value (example 3 or 4...), the input have to be reset t

I've been using host to plot my graphs, and I would like to take decimal numbers off from my x axis. My code below is kinda simplified, it represents what I need tho. I want the x-range to be exactly

Is it possible to format numbers with CSS? That is: decimal places, decimal separator, thousands separator, etc.

I am trying to check to see if my string only have numbers or decimals in it using JavaScript. If it has only numbers or decimals in the string then it should pass else it want to display a alert sayi

I am using JavaScript on my website to check input elements within a form. I am trying to ensure that there are no numbers in the field when the user enters their name. The form is created using PHP.

I'm having a problem where I am wanting to go through an array and check that only positive numbers have been entered. I know that it is possible to use isDigit from ctype.h but I'd rather construct s

I recently created a program that will create a math problem based on the user input. By entering either 1-4 the program can generate a problem or the user can quit by entering 5. The only problem I a

I would like to generate uniformly distributed random numbers between 0 and 0.5, but truncated to 2 decimal places. without the truncation, I know this is done by import numpy as np rs = np.random.Ran

I am trying to write a regular expression for accepting positive numbers from 1-99 and accepting decimals up to 2 places I have written this expression ([1-9]|[1-9][0-9])(.[0-9]{1,2})? But when I te

how to restrict user to enter only numbers with 6 digits and two decimal values in textbox using javascript?? //Function to allow only numbers to textbox function validate(key) { //getting key code of

I want to fetch the whole and decimal numbers from paragraph. I'm using jQuery and regular expression for this. here is my code: jQuery(#Content_Text p, #Content_Text td).html(function (index, value

Currently, I have the function: function TotalAmount(field, container) { var total = 0; field.each(function () { total += $(this).val() * 1; }); container.val(total.toFixed(2)); } Example(1): 55.00

I have a telephone input field. You should only type numbers, spaces or leave it empty. An idea how to build the regex?

I would like to ask how can I retrieve out the list of fibo list and then check whether does the input value by the user is inside the fibo list. a , b = 1, 1 while num <= sys.maxint: fibo == a ,

I have the following function which allows only numbers on keypress: $('input').keypress(function(e) { var a = []; var k = e.which; for (i = 48; i < 58; i++) a.push(i); if (!($.inArray(k,a)>=0))

How do you check if numbers in an array are equal? I have an array with 10 numbers, and I want to check if two or more of the numbers are equal. What is the best way to check this? Thanks.

When searching for ways to allow only numbers on a text input you usually get something like: function keyPressEvent (e) { var keyCode = e.which || e.keyCode; if (keyCode < 48 || keyCode > 57) {

The iPad does not have a Numpad keyboard like the iPhone/iPod does. I'm looking to find how I can restrict the user's keyboard to only accept values 0 through 9. I would imagine using UITextField's

I need to implement a functionality for a textbox to allow only numbers. I have written the following code, but using Ctrl + V we are able to paste text. How can we prevent this. $('.numeric-textbox')

How to save only the numbers after the comma with mysql functions? eg : 16.03 ----> 03

I have a requirement where i need to allow only numbers in text box. if user tries to enter any other character other than numbers then we need to cancel the event. Please help me how to achieve this?

Is there a way to typecast a variable to a number only? Something like $var=(int)$var;, but not for integers? $var=is_numeric($var)?$var:NULL; doesn't seem right. Would $var=1*$var; be best? Thanks

how to check input text before POST operation? it must be numbers only <form method=post action=credit.php> Сумма кредита: <input type=text name=sum> <br /> первый взнос: <

i am wrestling with my regex. I want to allow only letters and numbers and a dot in a username, and 2 to 20 chars long I thought of something like this [0-9a-zA-Z]{2,20} but then 21 chars is also ok,

I am using validation in GWT. I am having textbox. In that field it accepts only whole numbers and decimal values not other than special characters . I used regex pattern [0-9*] but it does not acce

I want a spark datagrid that allow a user to input numbers only. They can only input number with up to 5 decimal places. I am struggling with this in Spark. I can do it in Flex by using the itemEditEn

How can I make that a float number only have 4 decimal places at most. In my J2ME app I have two fields : unitPrice (4 decimal places) and quantity(3 decimal places) and when I multiply them I got num

How do i stop python from giving me an ValueError everytime I put a letter in? Input must be a number! Using Python 2.7.3

I have range of numeric values. I want check each number within a range is exists in another numerics list. I am using for loop, but it slows down my Application. public void ShowResults() { // The St

How can I restrict input to a text-box so that it accepts only numbers and the decimal point?

2.3 IDLE and im having problems i need to check if a number is between two numbers 10000 - 30000 if number >= 10000 and number >= 30000: print (you have to pay 5% taxes) it's not working too

I need a Regex that given the following Strings: 12.123.123/1234-11, 12.123123123411 or 1123123/1234-11. I could extract only the numbers(12123123123411); Pattern padrao = Pattern.compile(\d+)

how to force the EditText to accept only numbers.?

Like an unsigned field but only for negative numbers. I'm just curious.

I am using tesseract for iOS and I can't seem to figure out how to get it only to try and read numbers. I am using the newest english release downloaded from the tesseract website. Does anyone know ho

I've built an app that pulls contacts from your google account. However, I can only use contacts that have a phone number. I thought about pulling all the contacts and just filtering out those that do

Y axis tick label should only show non decimal values / whole numbers as as series . if i set set TickUnit to 1 it should be 1,2,3,4,5,.. if i set Unit Ticks to 2 ..2,4,6,8,.. if i set to 5 5,10,15,20

I'm struggling with string: some text [2string] some another[test] and another [4]; trying to quote every value but number within [], so it could be converted into some text ['2string'] some anot

This question already has an answer here: Format number to always show 2 decimal places 10 answers I have html table with numbers in cells (class numbers). Some numbers are whole numbers, oth

New to java, i am trying to get output of variables grossPayConv, taxesConv and netPayConv to have the output of XX.XX(having 2 places after the decimal). i believe i am casting wrong or something. i

I'm trying to show some data that's integers but the BarChart is using fractional values on the axis which makes no sense in this scenario. Is there any way to force the chart to only use integer valu

In a sequence of length n, where n=2k+3, that is there are k unique numbers appeared twice and three numbers appeared only once. The question is: how to find the three unique numbers that appeared on

hi all below code giving me contact names and phone numbers from contact list but i need only phone numbers how can i solve the issue so give me solution with full details without using android widget

I am creating one algorithm for kids that will create random numbers and showing 5 numbers in each row upto 6 rows.for whole numbers it is good.But when i am taking random numbers between a negative a

How do I convert a TextBox to int or is there a box inside WPF that supports only numbers?

I followed Apple's tutorial on obtaining a contact's phone number and it works great but it only detects phone numbers with the Home label. Here is my code: - (void)displayPerson:(ABRecordRef)person

My Regular Expressions knowledge is next to none but I'm having to have some client-side valiation against a text box which only allows numbers up to two decimal places with no other input. This scrip

Live Demo: http://jsfiddle.net/thisizmonster/DveuB/ How can I change this so that the input only allows the characters A-Z, a-z, 0-9 while typing, without using a regular expression?

Is there a nice way to show show decimal numbers with one decimal place all the time but without rounding? So you have the following numbers: 3.55 3.5 3 and I want them to show 3.5 3.5 3.0

here is my TextBox written in C# with Key Down event handler private void TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e) { //ONLY ACCEPTS NUMBERS char c = Convert.ToChar(e.