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

  • Home
  • SEARCH
  • 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 893891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:17:36+00:00 2026-05-15T14:17:36+00:00

I wrote this validation method but am having issues with it. function validate_password(pwd) {

  • 0

I wrote this validation method but am having issues with it.

function validate_password(pwd)
{
    var score = 0;

    // Min length is 8
    if (pwd.length<8)
        return false;

    // Is lower present?
    if (/[a-z]/g.test(pwd))
    {
        console.log('a-z test on "'+pwd+'":' + /[a-z]+/g.test(pwd));
        score++;
    }

    // Is upper present?
    if (/[A-Z]/g.test(pwd))
    {
        console.log('A-Z test on: "'+pwd+'":' + /[A-Z]+/g.test(pwd));
        score++;
    }

    // Is digit present?
    if (/\d/g.test(pwd))
    {
        console.log('digit test on: "'+pwd+'":' + /\d/g.test(pwd));
        score++;
    }

    // Is special char present?
    if (/\W/g.test(pwd))
    {
        console.log('spec char test on: "'+pwd+'":' + /\W/g.test(pwd));
        score++;
    }

    if (score>=3)
        return true;
    else
        return false;
}

This is what is written to the console:

>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
A-Z test on: "aaasdfF#3s":true
digit test on: "aaasdfF#3s":true
spec char test on: "aaasdfF#3s":true
true

>>> validate_password('aaasdfF#3s')
a-z test on "aaasdfF#3s":true
false

On the first try it seems to work as expected but when I call the method a 2nd time, it doesn’t work as expected.

So, my question is why are there differences between the results from the first try and the 2nd try?

Thanks! 🙂

  • 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-15T14:17:36+00:00Added an answer on May 15, 2026 at 2:17 pm

    See MDC documentation on test.

    When you want to know whether a pattern is found in a string use the test method (similar to the String.search method); for more information (but slower execution) use the exec method (similar to the String.match method). As with exec, test called multiple times on the same regular expression instance will advance past the previous match.

    The solution is to remove the global or g flag from your regexes:

    /[a-z]/ instead of /[a-z]/g, and so on.

    Consider this simple example to see why the problem exists:

    var l = /[a-z]/g;
    
    // initial search starts at the beginning, matches "a" and returns true
    l.test("a"); // true
    // since the first character matched, lastIndex moves to the next index - 1
    l.lastIndex; // 1
    
    // this time we pass a different string to the regex, but unfortunatly it
    // starts searching from the lastIndex which is 1. There are no lower case
    // letters from this point onwards (only "---"), so return value is false.
    l.test("x---"); // false
    // Since this search failed, lastIndex wraps around to the beginning, so the 
    // next search will work as expected
    l.lastIndex; // 0
    

    For your given input "aaasdfF#3s", the lower case [a-z] test would have succeeded 7 times as there are 7 lower case characters, but fail the 8th time. And again succeed from the 9th to 15th time, and so on. The other tests will fail every alternate time as there is only one of each type of character – "F", "#", and "3" and it wraps around lastIndex to 0 when the test fails.

    The problem seems to be stemming from the fact that state is preserved in those RegExp objects between function calls, meaning a RegExp object is only created once, and not every time the function is called. This little test confirms it:

    function RegExpStatePersistenceTest() {
        var regex = /[a-z]/g;
    
        regex.counter = regex.counter || 0;
        regex.counter++;
    
        console.log("counter:" + regex.counter);
    }
    
    RegExpStatePersistenceTest(); // counter: 1
    RegExpStatePersistenceTest(); // counter: 2
    RegExpStatePersistenceTest(); // counter: 3
    RegExpStatePersistenceTest();​ // counter: 4
    

    If a new object was explicitly created using new RegExp(..), then on each invocation of the function, a fresh new RegExp object will be created, and state will not be preserved between calls.

    Also see, why-regexp-with-global-flag-in-javascript-give-wrong-results

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

Sidebar

Ask A Question

Stats

  • Questions 481k
  • Answers 481k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ronald you can use the IHTMLDocument2.onkeydown event to intercept and… May 16, 2026 at 6:23 am
  • Editorial Team
    Editorial Team added an answer Yes, you need to use different images. iOS 4 will… May 16, 2026 at 6:23 am
  • Editorial Team
    Editorial Team added an answer You can make use of Environment variables during installation but… May 16, 2026 at 6:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.