/    Sign up×
Community /Pin to ProfileBookmark

Validation question

Hello,

First post here?

I am trying to do two validations at a time:

  • 1.

    I am trying to convert “,” to “.” before calculation. Then “.” to “,” after calculation. The reason behind this is we have “,” as decimal seperator (3,113 for example). It is likely possible that user will enter “,” as decimal seperator. This is solved.

  • 2.

    I am trying to restrict user input to numbers (1…0, 1..0 in Numeric pad, cursor keys, back key, comma and decimal key). Now here I don’t know what should I do and how to do that :/

  • The code for decimal seperator conversion is here.

    [quote]

    function calculateBMI() {
    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(“,”,”.”);

    var weight = eval(document.datacollectionentry.weight.value);
    var height = eval(document.datacollectionentry.height.value);
    var height2 = height / 100;
    var BMI = weight / (height2 * height2);
    document.datacollectionentry.bmi.value=custRound(BMI,1);
    document.datacollectionentry.bmi.value = document.datacollectionentry.bmi.value.replace(“.”,”,”);
    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(“.”,”,”);
    }

    function custRound(x,places) {
    return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places);
    }

    [/quote]

    And the function is returned like this:

    [quote]

    <input type=”text” name=”weight” size=”5″ maxlength=”5″ value=”” onblur=”calculateBMI()” />

    [/quote]

    I do know that I need to restrict user input before bmi function. I don’t know how to do that. Can somebody please help me here?

    to post a comment
    JavaScript

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @deep_dhyaniDec 20.2005 — you can do that:

    <input type="text" name="weight" size="5" maxlength="5" value="" onblur="this.value=this.value.replace(',','.')" />
    Copy linkTweet thisAlerts:
    @OombongoauthorDec 20.2005 — Thanks deep.dyhani for the reply.

    Actually I need to restrict the user to numbers, "," and "." characters only. I have made this function. It is working as a good validator but it is not doing the replacing job + the calculation job.

    Here is the code:


    function letternumber(e) {

    var key;

    var keychar;

    if (window.event)

    key = window.event.keyCode;

    else if (e)

    key = e.which;

    else

    return true;

    keychar = String.fromCharCode(key);

    keychar = keychar.toLowerCase();

    // control keys

    if ((key==null) || (key==0) || (key==8) ||

    (key==9) || (key==13) || (key==27) )

    return true;

    // alphas and numbers

    else if (((",.0123456789").indexOf(keychar) > -1)) {

    calculateBMI();

    }

    else

    return false;

    }



    function calculateBMI() {

    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(",",".");

    var weight = eval(document.datacollectionentry.weight.value);

    var height = eval(document.datacollectionentry.height.value);

    var height2 = height / 100;

    var BMI = weight / (height2 * height2);

    document.datacollectionentry.bmi.value=custRound(BMI,1);

    document.datacollectionentry.bmi.value = document.datacollectionentry.bmi.value.replace(".",",");

    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(".",",");

    return true;

    }

    function custRound(x,places) {

    return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places);

    }

    [/quote]


    and here is the html code:


    10. Height (cm):<input type="text" name="height" size="3" maxlength="3" value="" />

    11. Weight (kg):<input type="text" name="weight" size="5" maxlength="5" value="" onkeypress="return letternumber(event)" />

    12. BMI (kg/m&sup2?:<input type="text" name="bmi" size="4" maxlength="4" value="" READONLY />
    [/quote]
    Copy linkTweet thisAlerts:
    @konithomimoDec 20.2005 — Here is how to replace the commas with periods:
    function removeCommas( strValue ) {
    var objRegExp = /,/g; //search for commas globally
    //replace all matches with empty strings
    return strValue.replace(objRegExp,'.');
    }


    Just send it the value that you want to replace them in.

    Here is how to replace the periods with commas:
    function addCommas( strValue ) {
    var objRegExp = /./g; //search for commas globally
    //replace all matches with empty strings
    return strValue.replace(objRegExp,',');
    }


    And then add this to your input tags to allow only numbers, commas, and periods. The only glitch with the following code is that if a user enters in a period or comma and then attempts to input something other than a number, period, or comma then the period/comma will be removed along with the other character.
    onkeyup="this.value=this.value.replace(/D[^0-9^,^.]/,'')"
    Copy linkTweet thisAlerts:
    @OombongoauthorDec 21.2005 — Thanks koni for thr reply.

    My problem is solved, and here is what I did:

    I broke the code in two functions, in different script files.

    1st js file.

    function letternumber(e) {

    var key;

    var keychar;

    if (window.event)

    key = window.event.keyCode;

    else if (e)

    key = e.which;

    else

    return true;

    keychar = String.fromCharCode(key);


    // control keys

    if ((key==null) || (key==0) || (key==8) ||

    (key==9) || (key==13) || (key==27) )

    return true;

    // alphas and numbers

    else if (((",.0123456789").indexOf(keychar) > -1)) {

    return true;

    }

    else

    return false;

    }
    [/quote]


    second js file

    function calculateBMI() {

    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(",",".");

    var weight = document.datacollectionentry.weight.value;

    var height = document.datacollectionentry.height.value;

    var height2 = height / 100;

    var BMI = weight / (height2 * height2);

    document.datacollectionentry.bmi.value=custRound(BMI,1);

    document.datacollectionentry.bmi.value = document.datacollectionentry.bmi.value.replace(".",",");

    document.datacollectionentry.weight.value = document.datacollectionentry.weight.value.replace(".",",");

    return true;

    }

    function custRound(x,places) {

    return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places);

    }
    [/quote]


    And here is the HTML


    <tr><td>

    10. Height (cm):

    <input type="text" name="height" size="3" maxlength="3" value="" onblur= "calculateBMI()" onkeypress="return letternumber(event)" />

    </td></tr>

    <tr><td>

    11. Weight (kg):

    <input type="text" name="weight" size="5" maxlength="5" value="" onfocus="document.forms.datacollectionentry.weight.select()" onblur= "calculateBMI()" onkeypress="return letternumber(event)" />

    </td></tr>

    <tr><td>

    12. BMI (kg/m&sup2?:

    <input type="text" name="bmi" size="4" maxlength="4" value="" onblur= "calculateBMI()" READONLY />

    </td></tr>
    [/quote]
    ×

    Success!

    Help @Oombongo spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.1,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,
    )...