0
<script type="text/javascript">    
function allowDecimal(txt) {
        var theEvent = txt.htmlEvent || window.event;
        var key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode(key);
        var regex = /^\d*[0-9](|.\d*[0-9]|)*$/;

        if (!regex.test(key)) {
            theEvent.returnValue = false;
            if (theEvent.preventDefault)
                theEvent.preventDefault();
        }
    }

<asp:TextBox ID="txt" runat="server" onkeypress="allowDecimal(this);"></asp:TextBox>

これは.入力を許可していないので、何が問題なのか誰か助けてくれませんか

4

3 に答える 3

1

この正規表現を試してください

  <script type="text/javascript">    
   function allowDecimal(txt) {
    var theEvent = txt.htmlEvent || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode(key);
    var regex = /^\d*\.?\d*$/;

    if (!regex.test(key)) {
        theEvent.returnValue = false;
        if (theEvent.preventDefault)
            theEvent.preventDefault();
    }
}
</script>
于 2013-11-14T13:42:37.707 に答える
0

正規表現を使用せずに次のように書くこともできます。

var str = "hello.world"
var str2 = str.split(".");
var validate = str2.length;
if(validate != 1){
   alert("Decimal present");
}
else{
   alert("No decimal found");
}
于 2013-11-14T13:43:23.777 に答える
0
var decimal=/^[+-]?(\d+(\.\d+)?|\.\d+)$/;

これは (オプションの) 記号に一致します。

その後に任意の桁数が続き、オプションで小数点が続き、その後に数字が続きます。

または小数点の後に数字が続きます。

于 2013-11-14T14:36:42.063 に答える