代码之家  ›  专栏  ›  技术社区  ›  Dinesh Jayapal

Backspace在Mozila浏览器上的“我的表单”文本区域中不起作用

  •  1
  • Dinesh Jayapal  · 技术社区  · 11 年前

    function onlyAlphabets(e, t) {
      try {
        if (window.event)  {
          var charCode = window.event.keyCode;
        }
        else if (e) {
          var charCode = e.which;
        }
        else { return true; }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
          return true;
        else
          return false;
      }
      catch (err) {
        alert(err.Description);
      }
    }
    
    function isNumber(evt) {
      evt = (evt) ? evt : window.event;
      var charCode = (evt.which) ? evt.which : evt.keyCode;
      if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
      }
      return true;
    }   
           
    $('input.facebookUrl').keyup(function(){
      if (
        ($(this).val().length > 0) && ($(this).val().substr(0,24) != 'http://www.facebook.com/')
        || ($(this).val() == '')
      ){
        $(this).val('http://www.facebook.com/');    
      }
    });
    <table align="center">
      <tr>
        <td>
          <input name="firstname"  class="text_area"  maxlength=20 placeholder="Name" type="text" onKeyPress="return onlyAlphabets(event,this)" required  id="ValidName" value="" />
        </td>
      <tr>
        <td>
          <input type="text" onkeypress="return isNumber(event)" id="volpincode" maxlength=7 name="pincode"  value="" required  placeholder="Pincode" class="text_area">
        </td>
        <td><input style="width:300px" type="text" onkeypress="return keyup(event)" class="facebookUrl" name="facebook" value="http://www.facebook.com/$facebook"></td>
        <td>
          <input style="width:30px" disabled="disabled" type="text" name="facebook" value="+91" />
        </td>
      </tr>
      </tr>
    </table>

    我正在尝试验证名称和Pin码,以便在Name字段中只输入字母,而不输入任何其他字符,在Pincode中只应输入数字。该表单在Google chrome中运行良好,但当我输入并尝试删除文本字段中的文本时,firefox不会删除字符。

    2 回复  |  直到 11 年前
        1
  •  0
  •   Light    11 年前

    要仅验证名称的字母,请使用以下代码:

    $(document).on("keydown", "#NametextboxID", function (e) {
                    if (e.ctrlKey || e.altKey) {
                        e.preventDefault();
    
                    } else {
    
                        var key = e.keyCode;
                        if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90) || (key == 9))) {
                            e.preventDefault();
                        }
                    }
                });
    

    要仅验证Pin的数字,请使用以下代码:

     $(document).on("keydown", "#PinId", function (e) {
                    if (e.shiftKey || e.ctrlKey || e.altKey) { // if shift, ctrl or alt keys held down
                        e.preventDefault();         // Prevent character input
                    } else {
                        var n = e.keyCode;
                        if (!((n == 8)              // backspace
                                || (n == 46)                // delete
                                || (n >= 35 && n <= 40)     // arrow keys/home/end
                                || (n >= 48 && n <= 57)     // numbers on keyboard
                                || (n >= 96 && n <= 105)
                                || (n == 9))   // number on keypad
                        ) {
                            e.preventDefault();
                            // alert("in if");
                            // Prevent character input
                        }
                    }
                });
    
        2
  •  0
  •   Jyoti Prakash    11 年前

    为名称字段的“onlyAlphabets函数”再添加一个条件。

    if (charCode == 8 || (charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
    

    JS Fiddle Link