Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Wouter Benoit
Starting Member
23 Posts |
Posted - 2007-10-18 : 07:54:01
|
Im trying to create a regular expression to check if the user enters a numeric value in the text field.I use the following codeDim maskerbedrag As Regex = New Regex("(0-9)+[,]?(0-9)*")Dim bedrag As DecimalDim M As Match = maskerbedrag.Match(bedrag) If M.Success Then bedrag = txtOverschrijven.Text else msgbox("error in value textfield") end ifI read a document about regular expressions and as I understood it, my code should do the followingone or more numeric characters that might by followed by a comma and zero or more numeric characters.But when, for example, I fill in 15 or 15,5 in the text field, I get the error message I coded.How is this possible? I'm new to using regular expressions and I don't understand what went wrong.Can someone please explain or tell me what I do wrong.Thanks in advance |
|
Wouter Benoit
Starting Member
23 Posts |
Posted - 2007-10-18 : 08:02:46
|
OK, I just found one stupid mistake in my code and that is that I dont give the variable 'bedrag' a value before it is checked by the regular expression.So the code now is Dim maskerbedrag As Regex = New Regex("(0-9)+[,]?(0-9)*")Dim bedrag As Decimalbedrag = CDec(txtOverschrijven.Text)Dim M As Match = maskerbedrag.Match(bedrag)If M.Success Thenmsgbox("Succes")elsemsgbox("error in value textfield")end ifBut it still doesn't seem to work. |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-10-18 : 08:36:53
|
All you need to do is use one of the ASP.NET validation controls on the TextBox to check to see if it is a valid numeric. Like this:<asp:CompareValidator Runat="server" ControlToValidate="txtOverschrijven" ErrorMessage="Must be a number!" Type="Double" Operator="DataTypeCheck" ID="Comparevalidator1" >*</asp:CompareValidator>Read about that in the ASP.NET help pages or google it for more info.FYI -- What are you are trying to do doesn't making any sense. You have a string, from a text box. You are then converting that string to a decimal -- which will fail right there if the string isn't a valid number! Then, you are applying a regular expression check on the decimal, which has been implicitly converted BACK to a string. Also, you can just use the IsMatch(Value) property of the RegularExpression object to do a quick check to see if a value matches the pattern.But overall, don't use regular expressions for this -- use validation controls. You can also simply use Decimal.Parse() in a TRY block, or use Decimal.TryParse() in .net 2.0. For example:Dim Dec as decimal = 0Dim Valid as Boolean = FalseBegin Try Dec = Decimal.Parse(txtOverschrijven.Text) Valid = TrueCatch Ex As Exception Valid = FalseEnd TryIf Valid Then MsgBox("Success!")Else Msgbox("Error! Not Valid!")End If - Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
Wouter Benoit
Starting Member
23 Posts |
Posted - 2007-10-18 : 09:18:42
|
That works just fine.Thanks a lot Jeff |
|
|
|
|
|
|
|