Skip to main content
Inspiring
May 14, 2013
Question

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

  • May 14, 2013
  • 1 reply
  • 737 views

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

This topic has been closed for replies.

1 reply

Inspiring
May 14, 2013

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…