0

私はjsfiddleでうまく動作 するjQuery文字フィルターを使用しています - http://jsfiddle.net/itamarperetz/VgjJs/1/

私は自分のコンピューターで同じコードを使用しています(これまでのところ、xampp、apacheでコードを実行しています)が、まったく機能していません-必要な文字を入力できます(jsfiddle i'期待どおり、「.!$^$@$」などと入力できません。

※jQueryをインポートし、正常にインポートされていることを確認しました。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>

<script type="text/javascript">
/*
 * This plugin filters keyboard input by specified regular expression.
 * Version 1.7
 * $Id$
 *
 * Source code inspired by Ext.JS (Ext.form.TextField, Ext.EventManager)
 *
 * Procedural style:
 * $('#ggg').keyfilter(/[\dA-F]/);
 * Also you can pass test function instead of regexp. Its arguments:
   * this - HTML DOM Element (event target).
   * c - String that contains incoming character.
 * $('#ggg').keyfilter(function(c) { return c != 'a'; });
 *
 * Class style:
 * <input type="text" class="mask-num" />
 *
 * Available classes:
   * mask-pint:     /[\d]/
   * mask-int:      /[\d\-]/
   * mask-pnum:     /[\d\.]/
   * mask-money     /[\d\.\s,]/
   * mask-num:      /[\d\-\.]/
   * mask-hex:      /[0-9a-f]/i
   * mask-email:    /[a-z0-9_\.\-@]/i
   * mask-alpha:    /[a-z_]/i
   * mask-alphanum: /[a-z0-9_]/i
 */

(function($)
{
    var defaultMasks = {
        pint:     /[\d]/,
        'int':    /[\d\-]/,
        pnum:     /[\d\.]/,
        money:    /[\d\.\s,]/,
        num:      /[\d\-\.]/,
        hex:      /[0-9a-f]/i,
        email:    /[a-z0-9_\.\-@]/i,
        alpha:    /[a-z_]/i,
        alphanum: /[a-z0-9_]/i
    };

    var Keys = {
        TAB: 9,
        RETURN: 13,
        ESC: 27,
        BACKSPACE: 8,
        DELETE: 46
    };

    // safari keypress events for special keys return bad keycodes
    var SafariKeys = {
        63234 : 37, // left
        63235 : 39, // right
        63232 : 38, // up
        63233 : 40, // down
        63276 : 33, // page up
        63277 : 34, // page down
        63272 : 46, // delete
        63273 : 36, // home
        63275 : 35  // end
    };

    var isNavKeyPress = function(e)
    {
        var k = e.keyCode;
        k = $.browser.safari ? (SafariKeys[k] || k) : k;
        return (k >= 33 && k <= 40) || k == Keys.RETURN || k == Keys.TAB || k == Keys.ESC;
    };

        var isSpecialKey = function(e)
    {
        var k = e.keyCode;
        var c = e.charCode;
        return k == 9 || k == 13 || (k == 40 && (!$.browser.opera || !e.shiftKey)) || k == 27 ||
            k == 16 || k == 17 ||
            (k >= 18 && k <= 20) ||
            ($.browser.opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)))
            ;

        };

        /**
         * Returns a normalized keyCode for the event.
         * @return {Number} The key code
         */
        var getKey = function(e)
    {
        var k = e.keyCode || e.charCode;
        return $.browser.safari ? (SafariKeys[k] || k) : k;
        };

        var getCharCode = function(e)
    {
        return e.charCode || e.keyCode || e.which;
    };

    $.fn.keyfilter = function(re)
    {
        return this.keypress(function(e)
        {
            if (e.ctrlKey || e.altKey)
            {
                return;
            }
            var k = getKey(e);
            if($.browser.mozilla && (isNavKeyPress(e) || k == Keys.BACKSPACE || (k == Keys.DELETE && e.charCode == 0)))
            {
                return;
            }
            var c = getCharCode(e), cc = String.fromCharCode(c), ok = true;
            if(!$.browser.mozilla && (isSpecialKey(e) || !cc))
            {
                return;
            }
            if ($.isFunction(re))
            {
                ok = re.call(this, cc);
            }
            else
            {
                ok = re.test(cc);
            }
            if(!ok)
            {
                e.preventDefault();
            }
        });
    };

    $.extend($.fn.keyfilter, {
        defaults: {
            masks: defaultMasks
        },
        version: 1.7
    });

    $(document).ready(function()
    {
        var tags = $('input[class*=mask],textarea[class*=mask]');
        for (var key in $.fn.keyfilter.defaults.masks)
        {
            tags.filter('.mask-' + key).keyfilter($.fn.keyfilter.defaults.masks[key]);
        }
    });

})(jQuery);​
</script>




</head>
<body>
<label><input type="text" class="mask-alphanum" /> - mask-alphanum</label>
​
</body>
</html>

何が問題なのかわからない場合は、どんな提案でも役に立ちます。

前もって感謝します

4

1 に答える 1

1

ファイルに問題があるはずです。以下は機能します(ただし、クエリのコメントでIsaidが述べたように、電子メールの検証は厳しすぎます):

<!DCOCTYPE html>
<html>
  <head>
    <title>Test inputmask</title>
    <script type="application/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="application/javascript">
/*
 * This plugin filters keyboard input by specified regular expression.
 * Version 1.7
 * $Id$
 *
 * Source code inspired by Ext.JS (Ext.form.TextField, Ext.EventManager)
 *
 * Procedural style:
 * $('#ggg').keyfilter(/[\dA-F]/);
 * Also you can pass test function instead of regexp. Its arguments:
   * this - HTML DOM Element (event target).
   * c - String that contains incoming character.
 * $('#ggg').keyfilter(function(c) { return c != 'a'; });
 *
 * Class style:
 * <input type="text" class="mask-num" />
 *
 * Available classes:
   * mask-pint:     /[\d]/
   * mask-int:      /[\d\-]/
   * mask-pnum:     /[\d\.]/
   * mask-money     /[\d\.\s,]/
   * mask-num:      /[\d\-\.]/
   * mask-hex:      /[0-9a-f]/i
   * mask-email:    /[a-z0-9_\.\-@]/i
   * mask-alpha:    /[a-z_]/i
   * mask-alphanum: /[a-z0-9_]/i
 */

(function($)
{
    var defaultMasks = {
        pint:     /[\d]/,
        'int':    /[\d\-]/,
        pnum:     /[\d\.]/,
        money:    /[\d\.\s,]/,
        num:      /[\d\-\.]/,
        hex:      /[0-9a-f]/i,
        email:    /[a-z0-9_\.\-@]/i,
        alpha:    /[a-z_]/i,
        alphanum: /[a-z0-9_]/i
    };

    var Keys = {
        TAB: 9,
        RETURN: 13,
        ESC: 27,
        BACKSPACE: 8,
        DELETE: 46
    };

    // safari keypress events for special keys return bad keycodes
    var SafariKeys = {
        63234 : 37, // left
        63235 : 39, // right
        63232 : 38, // up
        63233 : 40, // down
        63276 : 33, // page up
        63277 : 34, // page down
        63272 : 46, // delete
        63273 : 36, // home
        63275 : 35  // end
    };

    var isNavKeyPress = function(e)
    {
        var k = e.keyCode;
        k = $.browser.safari ? (SafariKeys[k] || k) : k;
        return (k >= 33 && k <= 40) || k == Keys.RETURN || k == Keys.TAB || k == Keys.ESC;
    };

        var isSpecialKey = function(e)
    {
        var k = e.keyCode;
        var c = e.charCode;
        return k == 9 || k == 13 || (k == 40 && (!$.browser.opera || !e.shiftKey)) || k == 27 ||
            k == 16 || k == 17 ||
            (k >= 18 && k <= 20) ||
            ($.browser.opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)))
            ;

        };

        /**
         * Returns a normalized keyCode for the event.
         * @return {Number} The key code
         */
        var getKey = function(e)
    {
        var k = e.keyCode || e.charCode;
        return $.browser.safari ? (SafariKeys[k] || k) : k;
        };

        var getCharCode = function(e)
    {
        return e.charCode || e.keyCode || e.which;
    };

    $.fn.keyfilter = function(re)
    {
        return this.keypress(function(e)
        {
            if (e.ctrlKey || e.altKey)
            {
                return;
            }
            var k = getKey(e);
            if($.browser.mozilla && (isNavKeyPress(e) || k == Keys.BACKSPACE || (k == Keys.DELETE && e.charCode == 0)))
            {
                return;
            }
            var c = getCharCode(e), cc = String.fromCharCode(c), ok = true;
            if(!$.browser.mozilla && (isSpecialKey(e) || !cc))
            {
                return;
            }
            if ($.isFunction(re))
            {
                ok = re.call(this, cc);
            }
            else
            {
                ok = re.test(cc);
            }
            if(!ok)
            {
                e.preventDefault();
            }
        });
    };

    $.extend($.fn.keyfilter, {
        defaults: {
            masks: defaultMasks
        },
        version: 1.7
    });

    $(document).ready(function()
    {
        var tags = $('input[class*=mask],textarea[class*=mask]');
        for (var key in $.fn.keyfilter.defaults.masks)
        {
            tags.filter('.mask-' + key).keyfilter($.fn.keyfilter.defaults.masks[key]);
        }
    });

})(jQuery);

    </script>
  </head>
  <body>
    <div>
      Only alphabeta and numbers are allowed
      <label><input type="text" class="mask-alphanum" /> - mask-alphanum</label>
    </div>
  </body>
</html>
于 2012-08-13T12:54:36.997 に答える