Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression to accept only integers and decimal numbers

Tags:

regex

extjs

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??

like image 769
Spandana Jami Avatar asked Nov 05 '25 16:11

Spandana Jami


1 Answers

  • 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

like image 184
RaMeSh Avatar answered Nov 07 '25 10:11

RaMeSh