Check for numbers only !!!

# Thread: Check for numbers only !!!

1. Sid Guest

## Check for numbers only !!!

Hi i am using this ...<BR> if (theForm.cardNumber.value == "")<BR> {<BR> alert("Please enter a value for the \"Card Number\" field.");<BR> theForm.cardNumber.focus();<BR> return (false);<BR> }<BR><BR>anyone know how to check to make sure only numbers have been entered.<BR><BR>Thanks<BR>Sid

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## RE: Check for numbers only !!!

I tend to use:<BR><BR>var test = valToBeTested * 1.0;<BR>if ( isNAN(test) ) alert("Hey! That&#039s not a number!");<BR><BR>There are other tricks, but that one is so dirt easy...<BR><BR><BR>

3. Dan Evans Guest

## One extra thing

As neat and efficient as the above answer is, it&#039s unlikely that you want decimal points in the card number (Unless it&#039s a version number). In that case just add this line:<BR><BR>if (valToBeTested != Math.round(valToBeTested)) alert("Whole numbers only");

4. Sid Guest

## Help it don't work ;-()

Thanks for your suggestions but i tried this<BR> if (theForm.cardNumber.value != "")<BR> {<BR><BR>var test = (theForm.cardNumber.value) * 1.0; <BR>if ( isNAN(test) ) alert("Hey! That&#039s not a number!"); <BR>return (false);<BR>}<BR>and i came up with errors ..<BR>cardNumber.value is not an object.<BR><BR>any one know why , i am a bit new to this java type stuff.<BR>Sid

5. Dan Evans Guest

## Javascript is really picky

Watch your cases:<BR><BR>isNAN(test) won&#039t work while isNaN(test) will. Getting the case exactly right is an annoying feature of JavaScript, especially for those who grew up on VBasic<BR><BR>Hope this helps,<BR><BR>-Dan Evans

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•