Suraj
Suraj

Reputation: 11

Textbox that accepts all the characters but returns only numbers

I need the code to filter the data entered in a textbox. Although it accepts all the characters during runtime, the code should remove all the strings and alpha numeric characters except the numbers (which would be my output). I tried the following code but guess it won't do:

a = Textbox1.text
Dim value As Decimal = CDec(Regex.Replace(a, "[\D]", ""))

Upvotes: 1

Views: 293

Answers (3)

Peter Du
Peter Du

Reputation: 1312

Try this instead, use the matching object

Dim a As String
Try
    a = Regex.Match(Textbox1.text, "\d+").Value     
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

Upvotes: 0

Andy Davies
Andy Davies

Reputation: 4287

I use this jQuery plugin. http://plugins.jquery.com/project/jQueryNumberLettersPlugin

$("#id").numbers();

That would only allow numbers to be entered into the selected input.

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

Your regex was correct (just a bit redundant, \D would have done). Better would have been \D+ so consecutive non-decimals are replaced at once.

ResultString = Regex.Replace(SubjectString, "\D+", "")

Upvotes: 1

Related Questions