Randell D. wrote:
[...]
The values I'm talking about will not contain alphabetic characters -
thus numbers of 4.5e3 would not be expected but 4500 might be a
possibility.
Where I have a form called myForm, and an input tag named myAge, how
could I test that it is numeric?
Using the above example provided by Douglas (and after reading the url
he gave me) I have found the following conditional statement to return
false...everytime...
if(typeof docoument.myForm.myAge.value == 'number')
{ alert("true"); }
else
{ alert("false"); }
Because the values of form elements are always passed as
strings. However, JavaScript, being such a friendly language,
type converts where appropriate (or possible it seems to me). So
if the value "2" is entered into your myAge input, the following
will return true:
if (...myAge.value == '2' && ...myAge.value == 2)
The above is for test/example purpose only... could one of you fine
chaps let me know how I could test properly for this?
The best (only?) solution is to specify in your page the format
that you will accept and test for it. For age, I would expect
only a positive integer value, so something like:
<label for="myAge">Please enter your age at last
birthday:<input type="text" id="myAge"></label>
and perhaps a similar field for months if that level of accuracy
is required. Alternatively, ask for birth date (don't get
'them' started on dates). Some may claim even using the word
"birthday" is culturally insensitive for those who do not
recognise such things.
Based on the above, refuse any input that didn't pass being
tested for an optional sign "+" and only numbers. That may
tempt the more cantankerous twenty somethings to attempt say
0.028e3, but it's your form and you get to say what they can put
in it.
var a = ...myAge.value;
alert('Happy: ' + /^\s*[+]?\d+\s*$/.test(a));
which will allow leading & trailing space, optional leading "+"
and digits 0-9 and nothing else (lightly tested).
Replies via the newsgroup would be greatly appreciated so that all other
interested parties can learn to.
But of course.