Regular Expression to Check Only One Decimal Point

How to allow only one decimal point for input of type number in ionic 1

Try this Regex:

^\d*\.?\d+$

Click for Demo

Explanation:

  • ^ - Start of String
  • \d*- 0+ digits
  • \.?- 0 or 1 decimal point
  • \d+- 1+ digits(thus making it mandatory to have atleast one digit after the decimal point, if it is present)
  • $- End of String

OUTPUT:
enter image description here

regular expression to accept only integers and decimal numbers

  • Use \d* instead of \d+ before the decimal to match zero or more digits.
  • Also add anchors (^ and $) or else it will pass as long as there is any match
    available.
  • This would also validate an empty string, so if necessary you can use a

    lookahead to make sure there is at least one digit:

Use Below code:

 {
xtype: 'textfield',
id: 'myField',
fieldLabel: 'Text Field(numbers-only)',
maskRe: /^[1-9]\d*(\.\d+)?$/

}

Per your Understanding purpose see this link Click Here

Regular expression to match number with Decimal separator and optional Thousands separator

The reason the second alternative isn't matching is because it only allows a single \f after the decimal point. That needs to be \d+.

Then you need to wrap everything between ^ and $ in a group, so all alternatives match the entire string.

You had lots of redundant parentheses. And \d* in the last alternative should be \d+, otherwise you'll allow a number that's completely empty or just a sign.

^[+-]?([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+)$
  • ^ -> start of string
  • [+-]? -> matches optional + or - char
  • ([0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?|\d*\.\d+|\d+) -> whole group
    has to match [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) or \d*\.\d+ or \d+

    • [0-9]{1,3}(,[0-9]{3})*(\.[0-9]+) -> matches numbers with thousand separators and maybe decimal separator
    • \d*\.\d+ -> matches numbers with decimal separator, and maybe digits before the decimal
    • \d+ -> matches numbers without decimal separator
  • $ -> end of string

DEMO

Need a RegExp to filter out all but one decimal point

As I understand your question the code below might be what you are looking for:

var validatedStr=str.replace(/[^0-9.]|\.(?=.*\.)/g, "");

It replaces all characters other then numbers and dot (.), then it replaces all dots followed by any number of 0-9 characters followed by dot.


EDIT based on first comment - the solution above erases all dots but the last, the author wants to erase all but the first one:
Since JS does not support "look behind", the solution might be to reverse string before regex, then reverse it again or to use this regex:

var counter=0;
var validatedStr=str.replace(/[^0-9.]|\./g, function($0){
if( $0 == "." && !(counter++) ) // dot found and counter is not incremented
return "."; // that means we met first dot and we want to keep it
return ""; // if we find anything else, let's erase it
});

JFTR: counter++ only executes if the first part of condition is true, so it works even for strings beginning with letters

Allow only a single point in decimal numbers

Your regex doesn't matches what you say it matches. You have used negation in character class, and that too without any quantifier. Currently it would match any non-digit character other than ..

For your requirement, you can use this regex:

/^\d+(\.\d+)?$/

Regular expression for numbers and one decimal

Updated answer:

RegExp -

^(?:0|[1-9]\d+|)?(?:.?\d{0,2})?$

Explanation at regex101:

Sample Image

Original answer:

fiddle Demo

RegExp -

^(\d+)?([.]?\d{0,2})?$

Explanation

Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference number 1 «(\d+)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single digit 0..9 «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 2 «([.]?\d{0,2})?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the character “.” «[.]?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single digit 0..9 «\d{0,2}»
Between zero and 2 times, as many times as possible, giving back as needed (greedy) «{0,2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»


Related Topics



Leave a reply



Submit