在regex方面需要帮助

时间:2011-04-21 04:58:02

标签: javascript regex

此代码是否会阻止$%#或(因为我输入?

var digitsOnly = /[0-9]/g;
var emailOnly = /[a-zA-Z0-9_.@-]/g;
var alphaOnly = /[a-zA-Z]/g;
var dateOnly = /[0-9\/]/g;

function restrictKeys(myfield, e, restrictionType) {

    if (!e) var e = window.event
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);

    // if they pressed esc... remove focus from field...
    if (code==27) { this.blur(); return false; }

    // ignore if they are press other keys
    // strange because code: 39 is the down key AND ' key...
    // and DEL also equals .
    if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) {
        if (character.match(restrictionType)) {
            return true;
        } else {
            return false;
        }

    }
}

1 个答案:

答案 0 :(得分:2)

  

此代码是否会阻止$%#或(就像我一样   类型?

没有。检测按下的按键是徒劳的,用户可以将文本粘贴或拖动到表单控件中,因此键代码与输入的文本不匹配(或根本不触发键事件)。此外,您只关心提交表单时的值,与此同时,它与您无关。

在提交时验证表单控件内容,以任何方式限制键盘输入对用户来说非常非常烦人并且很容易被绕过。

相关问题