Using Validation controls to check for NON NUMERIC input. (This should be easy!)

Discussion in 'ASP .Net' started by Chad, Dec 28, 2005.

  1. Chad

    Chad Guest

    I have a textbox control, txtMeasurement, that I want to allow only numeric decimal input. I thought to use a client side validation control to ensure that the data entered is of type "Double".

    'Measurement Validation (Range Validator)
    Dim MeasurementRangeValidator As New RangeValidator 'validate that the measure is numeric

    MeasurementRangeValidator.ControlToValidate = txtMeasurement.ID|
    MeasurementRangeValidator.Display = ValidatorDisplay.Dynamic
    MeasurementRangeValidator.Type = ValidationDataType.Double|
    MeasurementRangeValidator.CssClass = "validatorerrormessage"
    MeasurementRangeValidator.MinimumValue = CType(-999999999999, Double).ToString 'essentially allow any value, we are just checking to make sure that it is numeric
    MeasurementRangeValidator.MaximumValue = CType(999999999999, Double).ToString

    However, if the user types a dash ("-") in the text box, the validation control's edit is passed but my program blows up on the following line:

    Dim dblMeasurement as Double = CTYPE(txtMEasurement.Text, Double)

    It fails because it is unable to convert a "-" to a Double data type!

    By the way, this also does not work:

    This function returns TRUE:

    BaseCompareValidator.CanConvert("-", ValidationDataType.Double)

    but

    Dim dblMyDouble As Double = CTYPE("-", Double)

    fails!

    Is there some way to get the Validation control to fire when and only when the user enters data that cannot be converted to a Double datatype?
     
    Chad, Dec 28, 2005
    #1
    1. Advertising

  2. Chad

    Peter Blum Guest

    The RangeValidator only validates once the data is considered well
    formatted. In the case of "-", its not a well formatted number.
    You need another validator to report a formatting error. That's the
    CompareValidator with its Operator=DataTypeCheck and Type=Double

    See this article for more on validation: http://aspalliance.com/699.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "Chad" <> wrote in message
    news:doudei$qb7$...
    I have a textbox control, txtMeasurement, that I want to allow only numeric
    decimal input. I thought to use a client side validation control to ensure
    that the data entered is of type "Double".
    'Measurement Validation (Range Validator)
    Dim MeasurementRangeValidator As New RangeValidator 'validate that the
    measure is numeric
    MeasurementRangeValidator.ControlToValidate = txtMeasurement.ID|
    MeasurementRangeValidator.Display = ValidatorDisplay.Dynamic
    MeasurementRangeValidator.Type = ValidationDataType.Double|
    MeasurementRangeValidator.CssClass = "validatorerrormessage"
    MeasurementRangeValidator.MinimumValue = CType(-999999999999,
    Double).ToString 'essentially allow any value, we are just checking to make
    sure that it is numeric
    MeasurementRangeValidator.MaximumValue = CType(999999999999,
    Double).ToString
    However, if the user types a dash ("-") in the text box, the validation
    control's edit is passed but my program blows up on the following line:
    Dim dblMeasurement as Double = CTYPE(txtMEasurement.Text, Double)
    It fails because it is unable to convert a "-" to a Double data type!
    By the way, this also does not work:
    This function returns TRUE:
    BaseCompareValidator.CanConvert("-", ValidationDataType.Double)
    but
    Dim dblMyDouble As Double = CTYPE("-", Double)
    fails!
    Is there some way to get the Validation control to fire when and only when
    the user enters data that cannot be converted to a Double datatype?
     
    Peter Blum, Dec 28, 2005
    #2
    1. Advertising

  3. Thanks for the response, but I regret to say that your suggestion did
    not work.

    The result was the same. The Comapre validation control thinks that "-"
    or "+" is a valid Double value. However, CTYPE cannot convert it to
    store in a Double variable.



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Chad Dokmanovich, Dec 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    386
  2. Replies:
    5
    Views:
    960
    X-Centric
    Jun 30, 2005
  3. Replies:
    8
    Views:
    349
    James Kanze
    Sep 25, 2007
  4. Replies:
    1
    Views:
    192
    Robert Klemme
    May 31, 2006
  5. John Smith
    Replies:
    8
    Views:
    185
    David K. Wall
    Sep 26, 2003
Loading...

Share This Page