如何在此开关案例中解析正确的值?

时间:2015-06-22 06:58:20

标签: javascript jquery parsing formatting switch-statement

我有一个带日期字段的格式。我希望在键入时自动格式化键入的值。

Ex:用户类型122,代码将其格式化为:12.02。我的问题是在案例1中,我试图阻止像32-39这样的一天输入,但用户仍然可以键入32+号码。我究竟做错了什么?

更新: 我发现这可以工作case 1: if (intKey >= 0 && ($(this).val()) != 3) { $(this).val(($(this).val())+key+"."); e.preventDefault(); }else if (key == "," || key == ".") { $(this).val("0"+($(this).val())+"."); e.preventDefault(); }
if (($(this).val()) == 3 && key > 1) { e.preventDefault(); } if (($(this).val()) == 3 && key == 0 || key == 1) { $(this).val(($(this).val())+key+"."); e.preventDefault(); } break;

if (($(this).val()) == 3 && key > 1) {
    e.preventDefault();
}



$(window).ready(function() {
  $('#date').keypress(function(e) {
    console.log($(this).val().length);
    var currentPosition = $(this).val().length
    var code = (e.keyCode ? e.keyCode : e.which);
    if (e.which != 8 && e.which != 0 && (e.which < 44 || e.which > 57)) {
      $(".errmsg").html("Bitte nur Zahlen eingeben").show().fadeOut(1700);
      return false;
    }
    var key = e.key;
    var intKey = parseInt(key)

    switch (currentPosition) {
      case 0:
        if (intKey != "NaN" && intKey > 3) {
          $(this).val("0" + key + ".");
          e.preventDefault();
        } else if (key == "," || key == ".")
          e.preventDefault();
        break;
      case 1:
        if (intKey >= 0) {
          $(this).val(($(this).val()) + key + ".");
          e.preventDefault();
        } else if (key == "," || key == ".") {
          $(this).val("0" + ($(this).val()) + ".");
          e.preventDefault();
        }
        if (($(this).val()) == 3 && key > 1) {
          e.preventDefault();
        }
        break;
        break;
      case 2:
        if (key == "," || key == ".") {
          e.preventDefault();
        }
        break;
      case 3:
        if (intKey != "NaN" && intKey > 1) {
          $(this).val(($(this).val()) + "0" + key + ".");
          e.preventDefault();
        } else if (key == "," || key == ".") {

          e.preventDefault();
        }
        break;
      case 4:
        if (intKey != "NaN" && intKey == 1 || intKey == 2 || intKey == 0) {
          $(this).val(($(this).val()) + key + ".");
          e.preventDefault();
        } else if (key == "," || key == ".") {
          $(this).val(($(this).val()).substring(0, $(this).val().length - 1) + "01" + ".");
          e.preventDefault();
        }
        break;
      case 5:
        if (key == "," || key == ".") {
          e.preventDefault();
        }
        break;
      case 6:
        if (intKey != "NaN" && intKey > 2 || intKey == 0) {
          e.preventDefault();
        } else if (intKey == 2) {
          $(this).val(($(this).val() + key) + "0");
        }
        if (key == "," || key == ".") {
          e.preventDefault();
        }
        break;
      case 7:
        if (key == "," || key == ".") {
          e.preventDefault();
        }
        break;
    }
  });
});
&#13;
.error {
  display: none;
  margin-left: 10px;
}
.error_show {
  color: red;
  margin-left: 10px;
}
input.invalid,
textarea.invalid {
  box-shadow: 0.0px 0.4px 3.1px 0.6px rgb(255, 0, 0)
}
input.valid,
textarea.valid {
  box-shadow: 1.0px 0.7px 7.2px 0.6px rgb(0, 100, 0));
}
.errmsg {
  color: red;
  font-style: oblique;
  font-size: 12px;
  font-family: fantasy;
}
&#13;
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<link href="error.css" rel="stylesheet" />
<script>
</script>

<body style="position:relative;">
  <form style="display:block;margin:0 auto;border:1px solid #aaa;border-radius:10px;width:230px;padding:10px;margin-top:150px;background-color:#F0F0F0;box-shadow:2px 2px 2px 2px rgba(0,0,0,0.2);">
    <table>
      <tr>
        <td>Datum:</td>
        <td>
          <input id="date" type="text" value="" maxlength="10" placeholder="tt.mm.jjjj" style=width:120px;margin-left:5px; />
        </td>
      </tr>

    </table>
  </form>
</body>
&#13;
&#13;
&#13;

0 个答案:

没有答案