Regular Expression

G

Guest

I have a textbox and want to allow my user to enter negative or positive
decimal values.

I want to do 2 things:

1) Only allow the user to type one minus sign (-)
2) Only allow the user to enter one point (.)

I would like to do this using Javascript or a Regular Expression.

Anyone got something that does this already?
 
G

Guest

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

That should do it - it will also prevent a decimal point if there's no
digits after.
 
K

Kevin Spencer

Slight revision:

^(-)?\d*\.?\d+$

Besides being a bit more succint, it allows a number that starts with a dot,
such as ".1" without a preceding digit.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

Keith said:
^(-)?[0-9]+(\.[0-9]+)?$

That should do it - it will also prevent a decimal point if there's no
digits after.

C said:
I have a textbox and want to allow my user to enter negative or positive
decimal values.

I want to do 2 things:

1) Only allow the user to type one minus sign (-)
2) Only allow the user to enter one point (.)

I would like to do this using Javascript or a Regular Expression.

Anyone got something that does this already?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top