Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Regex - Limit input to a positive number (float or int)

Explorer ,
May 14, 2013 May 14, 2013

Hi!

I wish to limit the user input of and edittext to only allow a positive number, float or int. I do not yet know my Regex good enough to do this. I realize that I must get better at regex but in the meantime does any one know how to do this ?

I have used this so far on the edittext:

function checkTextInput()

{

    if (this.text.match(/[^0-9.]/g))

    {

        this.text = this.text.replace(/[^0-9.]/g, "");

    } 

}

Which matches

000.0001

0.45.000.1

...1

which is not desired.

Desired is

0.n

.n

n.n

Best Regards,

Johan

TOPICS
Scripting
690
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guru ,
May 14, 2013 May 14, 2013
LATEST

Im not the greatest of regexers but may be… I perfer to use test() over match to return boolean… match can return null too so you need an if statement too…

function otherNumber( n ) { return /^\d*?\.\d$/.test( n ) };

Im sure jong will show a better way he's much better at these…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines