Skip to main content
Inspiring
June 6, 2008
Question

Help with Javascript - Numeric Entry Check

  • June 6, 2008
  • 3 replies
  • 863 views
I am struggling with a javascript and would like to ask for help from the community.

I have a form with several currency fields.. Its a very big form, so i wanted to check the user entry when the user moves to the next field using onBlur event, so that the user is not alerted of bad entries at the end.

VALID entires include:

0 to 10,
negative numbers
Decimal entries 55,555.66
$ 1,00,000
Blank entries (only if the user has marked the form as incomplete)

I have attached this test code for your reference. The problem with the code is that that it throws error when user enters for example 5,00,000 (commas)... or $5,000

If the user marks the form COMPLETE, then it should do a final check to check all the entries.

Can someone please help me with this?
    This topic has been closed for replies.

    3 replies

    Participating Frequently
    June 6, 2008
    I bought Dreamweaver CS3 about 2 months ago. I HIGHLY recommend it. The Spry Validation does exactly what you want. You can make a huge form with validation like credit card, email, integer, required... in minutes. I'm sure you can download just the spry validation JS and CSS files somewhere and you'll have all you need for all types of validation.
    Inspiring
    June 6, 2008
    Mark, thank you for the tip. I will look into upgrading soon, but can someone help me with this as i had to show a sample to my boss.

    thank you

    JT
    Inspiring
    June 6, 2008
    Thank you for your response Dan,

    I tried the code and got seveal errors...I have very limited knowledge of javascript i could not make it work... can you please resend a detailed code with the form etc? I am sorry that i am asking for spoon feeding...thank you in advance

    JT
    Inspiring
    June 6, 2008
    quote:

    Originally posted by: JethroTull
    Thank you for your response Dan,

    I tried the code and got seveal errors...I have very limited knowledge of javascript i could not make it work... can you please resend a detailed code with the form etc? I am sorry that i am asking for spoon feeding...thank you in advance

    JT

    It would confuse you more. I just read it again and it confused me. The page containing that function is data entry for a lund and browder chart and the validation was that the sum of two form fields could not exceed a certain maximum. The code I posted was to ensure that the form field was numeric before I tried to do math with it. It gets called by the js function that compares the sum to the allowable max.

    Feel free to post questions about any specific line.
    Inspiring
    June 6, 2008
    I had to do this once, and found things like NaN or parsefloat to be unreliable. So I wrote my own function.

    function isFloat(theString) {
    /*
    returns true if the input string is a float
    don't want to use parseFloat because for something like 12.2xs, parseFloat will return 12.2 which is not what we want
    */
    var i = 0;
    var theLength = theString.length;
    var thisChar = "";
    var usedDecimals = 0;
    var isAFloat = true;

    if (isAFloat == true) {
    for (i=0; i < theLength; i++) {
    thisChar = theString.substring(i, i + 1);
    // only allowed one decimal point
    if (thisChar == ".") {
    usedDecimals++;
    }

    if (usedDecimals > 1) {
    isAFloat = false;
    }

    if(isAFloat == true && thisChar != ".") {
    if (thisChar == "1" || thisChar == "2" || thisChar == "3" || thisChar == "4" || thisChar == "5"
    || thisChar == "6" || thisChar == "7" || thisChar == "8" || thisChar == "9" || thisChar == "0" ){
    var x = 1; // dummy statement for do nothhing
    } // numeric
    else {
    isAFloat = false;
    }
    } // usedDecimals ok
    } // looping through the string
    } // isAFloat is true
    return isAFloat;
    } // function is float