0

Basically, I have a text box that needs only decimal entries, but a validation rule that only allow decimals inside this text box.

Here is the code I have, but it does not do what I want:

#Region " RULE: Decimal Value Required Rule "

    Public Class DecimalRequiredRule
        Inherits Rules.BusinessRule

        Public Sub New(ByVal primaryProperty As Core.IPropertyInfo)
            InputProperties = New List(Of IPropertyInfo)({primaryProperty})
            Me.PrimaryProperty = primaryProperty
        End Sub

        Protected Overrides Sub Execute(ByVal context As Csla.Rules.RuleContext)
            Try
                Dim isDecimal As Decimal = CDec(context.InputPropertyValues(PrimaryProperty))
            Catch ex As Exception
                context.AddErrorResult(PrimaryProperty.FriendlyName & " must be a decimal")
            End Try
        End Sub
    End Class

#End Region
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
redoc01
  • 2,107
  • 5
  • 32
  • 64
  • What does it do? What type is the Primary Property? I'm guessing String. – Andy Sep 17 '13 at 19:23
  • I am not sure trying to use CDec is going to give the answer you want. The numbers 2 and 2999 will convert to decimal but do not have decimal POINTS which might be inferred from the mention of text boxes. The other question is what are you validating: user input in the TBs or data loaded externally for display? I might approach each differently. – Ňɏssa Pøngjǣrdenlarp Sep 17 '13 at 19:51
  • What do you want to accomplish? – Andy Oct 02 '13 at 02:30

1 Answers1

0

Assuming the PrimaryProperty is a backing for a string data type. I make this assumption because if it was a generics backed Decimal/Float/Double data type, then the data type would've already been "parsed" from the value bound in the text box.

As a string, you could simply do a check for the presence of a decimal point and this can be done in various ways like index of, or attempted <<valueType>>..TryParse methods, etc. and consequenly make the call to context.AddErrorResult(...) where appropriate.

If it is actually backed by Decimal/Float/Double, then you would need to use simple maths to check if the number has a non-zero value for the decimal portion. Various ways to do this would be to truncate the number and comparing whether the truncated number is the same value as the original and if not, there was a fraction, or even casting it to a data type with the integer portion that is big enough and compare that to the original.

Hope that helps.

Jaans
  • 4,598
  • 4
  • 39
  • 49