Spandana Jami
Spandana Jami

Reputation: 127

regular expression to accept only integers and decimal numbers

Iam working with ext js.I have a textfield that should accept either an integer or a decimal number. Iam using regular expression to implement that. But its not working.

Here is my code...

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

    }

While using the above regular expression, Textfield is not accepting .(dot)

How can I resolve this??

Upvotes: 4

Views: 12576

Answers (3)

RaMeSh
RaMeSh

Reputation: 3424

  • 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

Upvotes: 2

Toto
Toto

Reputation: 91385

Not sure I well understand your need, but is this OK?

/^[0-9]+(\.[0-9]*)?$/

This will accept:

  • 123
  • 0.123
  • 123.

Upvotes: 1

war1oc
war1oc

Reputation: 2755

you can use this if you want to limit the number of decimal places:

^\d+(\.\d{1,2})?$

this will let you pass decimal places at least 1 but not more than 2

Upvotes: 1

Related Questions