Alex Rivera | Logout

Javascript Regular Expressions - Replace non-numeric characters

Asked 2010-03-31T17:23:42.627
93

This works:

var.replace(/[^0-9]+/g, '');  

That simple snippet will replace anything that is not a number with nothing.

But decimals are real too. So, I'm trying to figure out how to include a period.

I'm sure it's really simple, but my tests aren't working.

Edit
Report

1 Answer

0

Sweet and short inline replacing of non-numerical characters in the ASP.Net Textbox:

 <asp:TextBox ID="txtJobNo" runat="server" class="TextBoxStyle" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" />

Alter the regex part as you'ld like. Lots and lots of people complain about the cursor going straight to the end when using the arrow keys, but people tend to deal with this without noticing it for instance, arrow... arrow... arrow... okay then... backspace back space, enter the new chars.

answered 2013-02-15T17:36:39.893

Your Answer