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

C

Chad

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?
 
P

Peter Blum

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: (e-mail address removed)
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

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?
 
C

Chad Dokmanovich

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top