Check for numbers only !!!

Results 1 to 5 of 5

Thread: Check for numbers only !!!

  1. #1
    Sid Guest

    Default 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. #2
    Join Date
    Dec 1969

    Default 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. #3
    Dan Evans Guest

    Default 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. #4
    Sid Guest

    Default 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. #5
    Dan Evans Guest

    Default 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