Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression ignore decimal 2

Status
Not open for further replies.

stewartwebb

Programmer
Jan 6, 2004
92
GB
Hi All,

I call this line of code on onKeyUp from a text box:-

Code:
myInput.value = myInput.value.replace(/\D*|./g, "")

The code works as I want it too, it removes any non numeric characters that have been entered.

Can someone let me know how I can adjust this line so that it removes non numeric characters but also ignores a decmial point?

Thanks

Stewart.
 
Thanks that works great.

1 more question, not sure if this is possible and sorry for not asking this in the first place, but is it possible to only allow 1 decimal point - at the moment the user can enter as many decimal points as they like?

Stewart.
 
You may try this.
[tt] myInput.value=myInput.value.replace(/((\.[^.]*?)+)(\.)/g,"$1").replace(/[^0-9.]/g,"");[/tt]
One detail of it is to forbid the last decimal point once there are one or more than one already entered. As it is implemented in the onkeyup, it effectively forbids the second decimal period.
 
One minor alternate suggestion to consider:

If you want to simplify work done by regular expression,
I would split the string on the decimal place character, and check the resulting array length (max 2).

Each array item can then be stripped of non-digits and the array re-joined into a string.

It is not as elegant as a regex but may be easier to maintain.



 
Thanks very much - works perfectly - you can both have a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top