Trying to remove the decimal point and everything after it

I have a value in an attribute named (data-max-value) which is currently set to 2.00000

The code below is trying to remove the .000000, leaving just 2 in this case.

Cant get this below to work.

var pointValueMaxScore = $(this).attr("data-max-value");
var pointValueMaxScoreRounded = $(pointValueMaxScore).html(Math.floor($(pointValueMaxScore).html()));

This is the whole function, so it gives it some perspective.

    $(document).on("click", "#txtNewQuestionChoiceScore", function () {
        var pointValueMaxScore = $(this).attr("data-max-value");
        var pointValueMaxScoreRounded = $(pointValueMaxScore).html(Math.floor($(pointValueMaxScore).html()));
        var pointValueCurrentScore = $(this).val();

        console.log(pointValueMaxScoreRounded);

        if (pointValueCurrentScore === pointValueMaxScoreRounded) {
            $(this).prop('disabled', true);
        }
    });

That looks like an overly complicated way to do things.

What about just using the following?

var pointValueMaxScoreRounded = Math.floor(pointValueMaxScore);
1 Like

Ah yes thanks Paul, “over complicating”, seems to be my middle name.

Thank you

Ye this isnt going to work, have just realised.

Basically the way I have it is once for example 2 = 2 I disable the input type number field, which works, but then the user hasnt the option to change 2 to 1.

How can I stop the incrementation option, once it gets to the cut off point, but allows the user to decrease the value.

This is how it is at the moment, but its not able to detect once 2 equals to 2 and then stop them counting up further.

$(document).on("click", "#txtNewQuestionChoiceScore", function () {
        var pointValueMaxScore = $(this).attr("data-max-value");
        var pointValueMaxScoreRounded = Math.floor(pointValueMaxScore);
        var pointValueCurrentScore = $(this).val();

        console.log(pointValueMaxScoreRounded);
        console.log(pointValueCurrentScore);

        if (pointValueCurrentScore >= pointValueMaxScoreRounded) {
            $("#txtNewQuestionChoiceScore").prop('disabled', true);
        } else {
            $("#txtNewQuestionChoiceScore").prop('disabled', false);
        }
    });

This is my input field

<input class="number-input  questionChoiceScoreInput textBoxSizeFullWidth textSizeStandard text-input formElement text-box single-line" data-decimal-places="5" data-max-value="2.00000" data-required="true" data-val="true" data-val-number="The field Score must be a number." id="txtNewQuestionChoiceScore" name="Score" type="number" value="" autocomplete="off" >

parseInt?

input type ‘number’ has an attribute, ‘max’ , that it enforces loosely. Also a step.

<input class="number-input questionChoiceScoreInput textBoxSizeFullWidth textSizeStandard text-input formElement text-box single-line" data-decimal-places="5" data-max-value="2.00000" data-required="true" data-val="true" data-val-number="The field Score must be a number." id="txtNewQuestionChoiceScore" name="Score" type="number" value="" autocomplete="off" max=2 step=0.00001 value=1.00000>

Validate on submit, rather than legislate on entry; or else disable keyboard input to the field entirely:

$("[type='number']").keydown(function (evt) {
    evt.preventDefault();
});
1 Like

Ye I understand, over complicated it again.

So what I have done now is stripped that function away, and on success load of the form added these few lines which stops the user entering anything they want in there, and also restricts the increment option on the number field, as below.

$("#txtNewQuestionChoiceScore").keydown(function (evt) {
        evt.preventDefault();
});            

var pointValueMaxScore = $("#txtNewQuestionChoiceScore").attr("data-max-value");
var pointValueMaxScoreRounded = Math.floor(pointValueMaxScore);

$("#txtNewQuestionChoiceScore").attr({
     "max": pointValueMaxScoreRounded,
     "min": 0
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.