Characters " ' " and " f " appear in the textbox, how to correct?

Asked

Viewed 63 times

4

We are using MVC5, I put a @Html.Textboxfor a Jquery function that does not allow the insertion of letters, but whenever the user type the characters " ' " and " f ", they are inserted and deleted in the act, the other characters are ok, nor are they typed, the problem is just these.

Just follow my lead:

   $(".input-numeric").numeric({
        decimal: 'false',
        negative:'false'
    });

Follow my cshtml input:

@Html.TextBoxFor(m => m.NumeroLogradouro, new { @class = "form-control input-numeric tam-6" })

3 answers

3

Apparently you’re using the package jQuery.Numeric. This package has bugs (see the section 'Notes', and see that really the character ' enters all fields). I would not use it if I were in your place.

There are two good alternatives:

0

The changes made to fix the Bug were:

Thiago Lunardi’s tip: No longer accepts the letter F, but continues to accept the " ' ".

$(".input-numeric").numeric({
    decimal: false,
    negative:false
});

Jquery Numeric (Old): Accepted " ' "

$.fn.numeric.keypress = function(e)
{
    // get decimal character and determine if negatives are allowed
    var decimal = $.data(this, "numeric.decimal");
    var negative = $.data(this, "numeric.negative");
    var decimalPlaces = $.data(this, "numeric.decimalPlaces");
    // get the key that was pressed
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    // allow enter/return key (only when in an input box)
    if(key == 13 && this.nodeName.toLowerCase() == "input")
    {
        return true;
    }
    else if(key == 13)
    {
        return false;
    }
    //dont allow #, $, %, '
    else if(key == 35 || key == 36 || key == 37){
        return false;
    }

Jquery Numeric (New) has been added to key 39, now locks the " ' ":

$.fn.numeric.keypress = function(e)
{
    // get decimal character and determine if negatives are allowed
    var decimal = $.data(this, "numeric.decimal");
    var negative = $.data(this, "numeric.negative");
    var decimalPlaces = $.data(this, "numeric.decimalPlaces");
    // get the key that was pressed
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    // allow enter/return key (only when in an input box)
    if(key == 13 && this.nodeName.toLowerCase() == "input")
    {
        return true;
    }
    else if(key == 13)
    {
        return false;
    }
    //dont allow #, $, %, '
    else if(key == 35 || key == 36 || key == 37 || key == 39){
        return false;
    }

Jquery Numeric Control+V (Old): Accepted Control+V

$.fn.numeric.blur = function()
{
    var decimal = $.data(this, "numeric.decimal");
    var callback = $.data(this, "numeric.callback");
    var negative = $.data(this, "numeric.negative");
    var val = this.value;
    if(val !== "")
    {
        var re = new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");
        if(!re.exec(val))
        {
            callback.apply(this);
        }
    }
};

Jquery Numeric (New): Now block Control+V

$.fn.numeric.blur = function()
{
    var decimal = $.data(this, "numeric.decimal");
    var callback = $.data(this, "numeric.callback");
    var negative = $.data(this, "numeric.negative");
    var val = this.value;
    if(val !== "")
    {
        var re = new RegExp("^" + (negative?"-?":"") + "\\d+$|^" + (negative?"-?":"") + "\\d*" + decimal + "\\d+$");
        if(!re.exec(val))
        {
            callback.apply(this);
            this.value = "";
        }
    }
};

-1


In Javascript that 'false' with single quotation marks or "false" are strings, and every string is FALSE.

$(".input-numeric").numeric({
    decimal: false,
    negative:false
});

Here is some very didactic material on the subject: W3schools: Javascript Booleans.

  • 2

    Good afternoon Thiago, would it be nice to explain this difference to the author right? I hope you take this as a constructive criticism :) - I recommend you read: http://answall.com/help/how-to-answer - Being brief is acceptable, but more thorough explanations are better.

  • @Thiago Lunardi, got it, you’re right, it really works, but the " ' " is still allowed, Gypsy Morrison Mendez said it’s a bug of the Numeric package, but I can’t use Type="Number" because the number has incompatibilities with IE9... Thanks for the help I’ll try to block the " ' ".

  • @Guilhermenascimento, I gave an increase. Thank you.

  • Thiago was fine, but I didn’t understand: every string is FALSE, what does this mean? This makes no sense, could it explain? (ps: It was not I who denied your answers)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.