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

4

We are using MVC5, I put a field @ Html.TextBoxFor a Jquery function that does not allow the insertion of letters, however whenever the user types the characters "'" and "f", they are inserted and deleted in the act, the other characters are okay, not even typed, the problem is just that.

Here's my function:

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

Here is my input in cshtml:

@Html.TextBoxFor(m => m.NumeroLogradouro, new { @class = "form-control input-numeric tam-6" })
    
asked by anonymous 26.11.2015 / 20:12

3 answers

-1

In JavaScript that 'false' with single quotes or "false" are strings, and every string is FALSE .

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

Here's a good teaching material on the subject: W3Schools: JavaScript Booleans .

    
26.11.2015 / 20:30
3

Apparently you're using the jQuery.Numeric package. This package has bugs ( see the 'notes' section , and see that the ' enters all fields). He would not use it if he were in his place.

There are two good alternatives:

26.11.2015 / 22:36
0

The changes made to the bug fix were:

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

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

Jquery Numeric (Old): Accepted 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){
        return false;
    }

Jquery Numeric (New) has been added to key 39, now blocks 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 blocks 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 = "";
        }
    }
};
    
27.11.2015 / 12:49