slymnozdmrc
slymnozdmrc

Reputation: 400

Html.TextBoxFor max length only count numbers mvc

I want to only count numbers but my textarea count also counts comma with number. How can I solve this problem?

@Html.TextBoxFor(m => m.Money, new { @class = "w90 amount-align",
                                     @id = "input-amount", 
                                     @maxlength = 11",
                                     @data_mask = "###.###.###", 
                                     @data_mask_reverse = "true",
                                     @Placeholder = "0" })

Upvotes: 0

Views: 2884

Answers (1)

Thiago Lunardi
Thiago Lunardi

Reputation: 749

Forget it. The maxlength input attribute only work with the input value char count. Wherever if it is number, currency, text, etc.

Then, you will need to develop your own customMaxLength by your self. Something like this.

HTML:

<input type="text" id="field" class="maxlength-5">

jQuery:

$(function(){
    $('.maxlength-5').keypress(function(evt){
        var maxLength = 5; // as it threat maxlength-5 custom class :)

        // assuming that data-mask will certify that only currency format will be valid        
        var pureValue = $(this).val().replace('.','').replace(',',''); 

        if(pureValue.length >= maxLength)
            evt.preventDefault();
});

});

Try it yourself here.

Upvotes: 1

Related Questions