For getting the float value from the webform (client-side event), I do use the code like:
var price = parseFloat(s.GetText());
if (!isNaN(price)) {
// calculate the related values
}
It works fine when the user enters a number (correctly). The isNaN()
works detects fine also the situation when the user types a string instead of a number (that is, when the first character does not belong to a number).
When the user types the value like 5 x
, the 5 is converted correctly because the parser stops on x
, and ignores the existence of the x
. So, the unwanted string after the number is not detected. However, storing the field content 5 x
into the database fails -- because the other code of the web page parses the value differently, and it requires it to be a number and nothing more.
How would you implement the check?