最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Preventing user to enter more numbers after 2 digits before decimal point - Stack Overflow

programmeradmin2浏览0评论

I'm having a problem with letting users enter numeric values in my textboxes which have the same class.

What I Want:

  • Allow users to enter only 2 numbers before the decimal points and another 2 after it; otherwise all keys are log except arrows keys, backspace, and delete key

Current Issue

  • Users can only enter 2 numbers; however, after he/she adds a decimal points, they can add more numbers before it
  • Currently, users can only enter 5 digits--2 before the decimal point, and another 2 after the decimal points. When they delete one digit after the decimal point, they can still add more number to digit before that decimal point.

My HTML:

<form method="post">
    <div class="row">
        <label>Val 1</label>
        <input type="text" class="validate" name="val1" maxlength="5" />
    </div>
    <div class="row">
        <label>Val 2</label>
        <input type="text" class="validate" name="val2" maxlength="5" />
    </div>
    <div class="row">
        <label>Val 3</label>
        <input type="text" class="validate" name="val3" maxlength="5" />
    </div>
    <div class="row">
        <button type="submit">Send</button>
    </div>
</form>

My JS:

Number.prototype.between  = function (a, b, inclusive) {
        var min = Math.min.apply(Math, [a,b]),
            max = Math.max.apply(Math, [a,b]);
        return inclusive ? this >= min && this <= max : this > min && this < max;
    };  
$('.validate').keypress( function( event ) {
        var v = parseFloat($(this).val() + String.fromCharCode(event.which));

        textValue = parseFloat(v).toString();
        var newVal = parseInt( textValue.substr(0, textValue.indexOf(".")) );

        console.log(newVal);
        if(textValue.length < 6){
           if(!parseFloat(v).between(0,99.99,true)) {
                v = this.value.substring(0, 2);
                $(this).val(v);
                return false;
            } 
            return true;
        }
    });

Here is the link to my fiddel DEMO

I'm having a problem with letting users enter numeric values in my textboxes which have the same class.

What I Want:

  • Allow users to enter only 2 numbers before the decimal points and another 2 after it; otherwise all keys are log except arrows keys, backspace, and delete key

Current Issue

  • Users can only enter 2 numbers; however, after he/she adds a decimal points, they can add more numbers before it
  • Currently, users can only enter 5 digits--2 before the decimal point, and another 2 after the decimal points. When they delete one digit after the decimal point, they can still add more number to digit before that decimal point.

My HTML:

<form method="post">
    <div class="row">
        <label>Val 1</label>
        <input type="text" class="validate" name="val1" maxlength="5" />
    </div>
    <div class="row">
        <label>Val 2</label>
        <input type="text" class="validate" name="val2" maxlength="5" />
    </div>
    <div class="row">
        <label>Val 3</label>
        <input type="text" class="validate" name="val3" maxlength="5" />
    </div>
    <div class="row">
        <button type="submit">Send</button>
    </div>
</form>

My JS:

Number.prototype.between  = function (a, b, inclusive) {
        var min = Math.min.apply(Math, [a,b]),
            max = Math.max.apply(Math, [a,b]);
        return inclusive ? this >= min && this <= max : this > min && this < max;
    };  
$('.validate').keypress( function( event ) {
        var v = parseFloat($(this).val() + String.fromCharCode(event.which));

        textValue = parseFloat(v).toString();
        var newVal = parseInt( textValue.substr(0, textValue.indexOf(".")) );

        console.log(newVal);
        if(textValue.length < 6){
           if(!parseFloat(v).between(0,99.99,true)) {
                v = this.value.substring(0, 2);
                $(this).val(v);
                return false;
            } 
            return true;
        }
    });

Here is the link to my fiddel DEMO

Share Improve this question asked Sep 26, 2013 at 6:09 Sophea PhySophea Phy 3883 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Try this :

   <input type="text" class="validate" name="val1" maxlength="5" onkeypress="return CheckDecimalValues(event)" />

function CheckDecimalValues(evt) {
    var keyCode = evt.keyCode ? evt.keyCode : ((evt.charCode) ? evt.charCode : evt.which);
    //    8   Backspace
    //    9   Tab key 
    //    46  Delete
    //    35  End Key
    //    36  Home Key
    //    37  Left arrow Move
    //    39  Right arrow Move
    if (!(keyCode >= 48 && keyCode <= 57)) {
        if (!(keyCode == 8 || keyCode == 9 || keyCode == 35 || keyCode == 36 || keyCode == 37 || keyCode == 39 || keyCode == 46)) {
            return false;
        }
        else {
            return true;
        }
    }

    var velement = evt.target || evt.srcElement
    var fstpart_val = velement.value;
    var fstpart = velement.value.length;
    if (fstpart .length == 2) return false;
    var parts = velement.value.split('.');
    if (parts[0].length >= 14) return false;
    if (parts.length == 2 && parts[1].length >= 2) return false;
}

Why don't you use a regex to validate your requirement?

A simple regex like so:

[0-9]{1}[0-9]{1}[\.]{1}[0-9]{1}[0-9]{1}

can be used to validate the input without worrying about the key strokes. Just add an onblur event on your textbox to validate this.

EDIT:

I have added this Fiddle

Check it out.

发布评论

评论列表(0)

  1. 暂无评论