zoukankan      html  css  js  c++  java
  • Jquery限制文本框输入

    引用 :http://www.cnblogs.com/xdp-gacl/p/3467245.html


    // ----------------------------------------------------------------------
    // <summary>
    // 限制只能输入数字
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.onlyNum = function () {
    $(this).keypress(function (event) {
    var eventObj = event || e;
    var keyCode = eventObj.keyCode || eventObj.which;
    if ((keyCode >= 48 && keyCode <= 57))
    return true;
    else
    return false;
    }).focus(function () {
    //禁用输入法
    this.style.imeMode = 'disabled';
    }).bind("paste", function () {
    //获取剪切板的内容
    var clipboard = window.clipboardData.getData("Text");
    if (/^d+$/.test(clipboard))
    return true;
    else
    return false;
    });
    };

    二、限制只能输入字母

    // ----------------------------------------------------------------------
    // <summary>
    // 限制只能输入字母
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.onlyAlpha = function () {
    $(this).keypress(function (event) {
    var eventObj = event || e;
    var keyCode = eventObj.keyCode || eventObj.which;
    if ((keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
    return true;
    else
    return false;
    }).focus(function () {
    this.style.imeMode = 'disabled';
    }).bind("paste", function () {
    var clipboard = window.clipboardData.getData("Text");
    if (/^[a-zA-Z]+$/.test(clipboard))
    return true;
    else
    return false;
    });
    };

    三、 限制只能输入数字和字母

    // ----------------------------------------------------------------------
    // <summary>
    // 限制只能输入数字和字母
    // </summary>
    // ----------------------------------------------------------------------
    $.fn.onlyNumAlpha = function () {
    $(this).keypress(function (event) {
    var eventObj = event || e;
    var keyCode = eventObj.keyCode || eventObj.which;
    if ((keyCode >= 48 && keyCode <= 57) || (keyCode >= 65 && keyCode <= 90) || (keyCode >= 97 && keyCode <= 122))
    return true;
    else
    return false;
    }).focus(function () {
    this.style.imeMode = 'disabled';
    }).bind("paste", function () {
    var clipboard = window.clipboardData.getData("Text");
    if (/^(d|[a-zA-Z])+$/.test(clipboard))
    return true;
    else
    return false;
    });
    };

    调用方法:首先在画面加载完成之后编写如下的JS脚本

    $(function () {
    // 限制使用了onlyNum类样式的控件只能输入数字
    $(".onlyNum").onlyNum();
    //限制使用了onlyAlpha类样式的控件只能输入字母
    $(".onlyAlpha").onlyAlpha();
    // 限制使用了onlyNumAlpha类样式的控件只能输入数字和字母
    $(".onlyNumAlpha").onlyNumAlpha();
    });

    对需要做输入控制的控件设置class样式

      <ul>
            <li>只能输入数字:<input type="text" class="onlyNum" /></li>
            <li>只能输入字母:<input type="text" class="onlyAlpha" /></li>
            <li>只能输入数字和字母:<input type="text" class="onlyNumAlpha" /></li>
      </ul>
  • 相关阅读:
    三、DQL语言
    四、DML语言
    五、DDL语言
    Set WebClient Timeout and reference Stackoverflow
    C# anonymous types and serialization
    C# get multiple folders size under upper folder and order them by size descendingly.
    C# NamedPipeServerStream NamedPipeClientStream
    Convert XLSX file to DataTable via NPOI
    C# NPOI read excel files include xls and xlsx
    C# write stream to local file
  • 原文地址:https://www.cnblogs.com/zhangsheng113/p/8663384.html
Copyright © 2011-2022 走看看