Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:40:59+00:00 2026-05-22T11:40:59+00:00

I am trying to do some validation check to the password field a form.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:41:00+00:00Added an answer on May 22, 2026 at 11:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do some very simple form validation checking for null or
I'm trying to use jQuery validation for a dynamic form I'm setting up. In
I’m thinking about trying some development for the iPhone, is it possible to install
I'm trying some of the ASP.NET MVC tutorials and one of them has the
I'm a beginner C++ programmer, and to stretch my mind I've been trying some
I've started with ASP.NET MVC recently, reading blogs, tutorials, trying some routes, etc. Now,
I have just started learning Erlang and am trying out some Project Euler problems
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Hey having some trouble trying to maintain transparency on a png when i create
When trying to link some well established tools to my company's active directory, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.