#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Kuala Lumpur, Malaysia
    Posts
    61
    Rep Power
    11

    Smile need help in javascript function to detect float number


    hi all. my first time here.
    hey, can anyone help me on this:
    i wanna check if a field contains integer value. if not, return false n stop the form from submissing. but i can oni manage to detect non-float numbers, means once the function detects the decimal point it will treat the whole thing as non-integer. how can i make this float number recognized as integer and continue the form submission?
    The longest distance in this world, is neither the distance from the Northern Hemisphere to the Southern Hemisphere, nor the distance between life and death, but is just there when we are face to face, and you don't even know I love you.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    try something like the following:

    Code:
    <form action="javascript:alert('Yay!');"
    onsubmit="return !isNaN(parseFloat(this.form.num.value));">
    <input type="text" value="10.45" name="num">
    <input type="submit">
    </form>
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Kuala Lumpur, Malaysia
    Posts
    61
    Rep Power
    11
    thanks for your reply kicken.
    but can u plz explain this line for me, cuz i dun reli understand it, thanks:
    isNaN(parseFloat(this.form.num.value))
    thanks,
    mao
  6. #4
  7. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,932
    Rep Power
    4033
    Sure. parseFloat take a string and tries to create a float number out of it. Since it's looking for a float, decimal points won't hurt it any. If it can't create a number from the string, it'll return a special value in javascript called NaN (Not a Number). The isNaN function checkes to see if this value exists.

    Since it'll return true if parseFloat returns NaN, we use the ! operator to reverse that so that it turns false if parseFloat returns NaN. That make it return the right result in the onSubmit handler.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Kuala Lumpur, Malaysia
    Posts
    61
    Rep Power
    11
    ic....thank you very much kicken.
    u've widen my knowledgebase indeed.

IMN logo majestic logo threadwatch logo seochat tools logo