如何处理两个javascript事件?

发布于 2024-11-08 07:06:16 字数 1914 浏览 0 评论 0原文

我正在尝试对表单的密码字段进行一些验证检查。我正在调用函数 onkeypress 来检查 capsLock 是否打开或关闭,然后调用 onblur 函数检查密码字段值。但是当我在密码字段中输入任何值时,onblur 会立即与 onkeypress 一起运行。这违背了我检查大写锁定然后检查字段值的目的。

HTML:

<input type="password" size=50 name="password" value="" onkeypress="checkCapsLock(event);" onblur="chkPsw(this.value);">

JavaScript:

function chkPsw(inpsw){
    var psw = inpsw.toLowerCase(inpsw);
    psw = trim(psw);
    if (psw.length<4 || psw.length>15){
        alert ('password length can be min. 4 and max. 15 chars' );
        return false;
    }
    var p1=/^[a-z0-9_\-\!\@\$\%\&\(\)\{\}\[\]\<\>]+$/;/* a-z 0-9 _ - ! @ $ % & ( ) { } [ ] < > */
    if(p1.test(psw)) {
        alert("The password:::: "+psw+" :::: is ok.");
        return true;
    } else {
        var p2 = /\s+/;
        if (p2.test(psw)){
            alert(psw+" is not ok. Space is not allowed.");
            return false;
        } else{
            alert(psw+"\n is not ok only a-z 0-9 _ - ! @ $ % & ( ) { } [ ] < > ");
            return false;
        }
    }
}


function checkCapsLock( e ) {
    var myKeyCode=0;
    var myShiftKey=false;
    var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

    // Internet Explorer 4+
    if ( document.all ) {
        myKeyCode=e.keyCode;
        myShiftKey=e.shiftKey;
    }

    if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
        alert( myMsg );
        return false;
    } else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
        alert( myMsg );
        return false;
    } else {
        return true;
    }
}

我想我已经说得很清楚了。如果有人能帮助我那就太好了。

我想要的是,当有人开始在此密码字段中输入时,将检查 capsLock 的状态并将其告知用户,然后当填写完整的字段并且用户移至下一个字段时,将检查密码值。

I am trying to do some validation check to the password field a form. I am calling a function onkeypress to check whether capsLock is on or off then onblur function check the password field value. But as I enter any value in the password field, the onblur runs immediately along with onkeypress. This defeats my purpose of checking the capslock and then the field value.

HTML:

<input type="password" size=50 name="password" value="" onkeypress="checkCapsLock(event);" onblur="chkPsw(this.value);">

JavaScript:

function chkPsw(inpsw){
    var psw = inpsw.toLowerCase(inpsw);
    psw = trim(psw);
    if (psw.length<4 || psw.length>15){
        alert ('password length can be min. 4 and max. 15 chars' );
        return false;
    }
    var p1=/^[a-z0-9_\-\!\@\$\%\&\(\)\{\}\[\]\<\>]+$/;/* a-z 0-9 _ - ! @ $ % & ( ) { } [ ] < > */
    if(p1.test(psw)) {
        alert("The password:::: "+psw+" :::: is ok.");
        return true;
    } else {
        var p2 = /\s+/;
        if (p2.test(psw)){
            alert(psw+" is not ok. Space is not allowed.");
            return false;
        } else{
            alert(psw+"\n is not ok only a-z 0-9 _ - ! @ $ % & ( ) { } [ ] < > ");
            return false;
        }
    }
}


function checkCapsLock( e ) {
    var myKeyCode=0;
    var myShiftKey=false;
    var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

    // Internet Explorer 4+
    if ( document.all ) {
        myKeyCode=e.keyCode;
        myShiftKey=e.shiftKey;
    }

    if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
        alert( myMsg );
        return false;
    } else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
        alert( myMsg );
        return false;
    } else {
        return true;
    }
}

I think I made myself clear. If any one can help me that would be great.

What I want is when someone starts typing in this password field the status of capsLock is checked and told to the user and then when the complete field is filled out and the user moves to next field, the password value is checked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-11-15 07:06:16

好吧,问题是您使用警报框来通知用户(不好的做法,而且很烦人),这会导致密码字段失去焦点。

解决方案是什么?使用布尔条件,

这是一个 jQuery 示例(我正在清理您的代码),

jsFiddle: http://jsfiddle.net /MnMps/

jQuery.fn.caps = function(cb){
    return this.keypress(function(e){
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};

var alerting = false;

$('#password').caps(function(caps){
    if(caps){
        alerting = true;
        alert('Unintentional uppercase leads to wars!');
       }
});

$('#password').blur(function() {
    if(alerting == false && !this.value.match(/^[a-z0-9_\-\!\@\$\%\&\(\)\{\}\[\]\<\>]+$/)) {
        alert("Abort! Invalid character detected!");
    }
    alerting = false;
});

PS: 大写锁定检测的道具, http://plugins.jquery.com/plugin-tags/caps-lock

编辑:放弃清理代码。

Well, the problem is that you're using alert boxes to notify the user (bad practice, and annoying) which results in the password field losing focus.

The solution? Use a boolean condition,

Here's a jQuery example (I'm cleaning up your code),

jsFiddle: http://jsfiddle.net/MnMps/

jQuery.fn.caps = function(cb){
    return this.keypress(function(e){
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};

var alerting = false;

$('#password').caps(function(caps){
    if(caps){
        alerting = true;
        alert('Unintentional uppercase leads to wars!');
       }
});

$('#password').blur(function() {
    if(alerting == false && !this.value.match(/^[a-z0-9_\-\!\@\$\%\&\(\)\{\}\[\]\<\>]+$/)) {
        alert("Abort! Invalid character detected!");
    }
    alerting = false;
});

PS: Props to capslock detection, http://plugins.jquery.com/plugin-tags/caps-lock

EDIT: Gave up on cleaning up your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文