RegExp.test not giving correct result










0














In the following code, I pass a FormControl which contains a password. I expect that when the password is aA1[11], the RegExp.test method should return false but it returns true! Why my code is returning null instead of error object
validatePassword:
valid: false,
message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'



Shouldn't this forward look up fail the match (?=.*[!@#$%^&*()_+":'?&gt.<,])



 validatePassword(control: FormControl) 

let password: string = control.value;
/* So the rule for password is
6-10 length
contains a digit
contains a lower case alphabet
contains an upper case alphabet
contains one more special character from the list !@#$%^&*()_+":;'?/>.<,
does not contain space
*/
let REG_EXP = new RegExp('(?=^.6,10$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+":'?&gt.<,])(?!.*\s).*$');
/*RegExp's test method returns true if it finds a match, otherwise it returns false*/
console.log('password: ',password);
console.log('test result ',(REG_EXP.test(password)));
return (REG_EXP.test(password)) ? null :
validatePassword: //check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'





I am calling the above function from my Karma test case



fit('A password of length between 6-10 characters and containing at least 1 digit, at least 1 lowercase letter, at least 1 upper case ' +
'letter and but NOT at least 1 special character from the list !@#$%^&*()_+{":;'?/>.<, shall NOT be accepted',
inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>
let helper = new HelperService(loaderService,httpClient);
let passwordField = new FormControl();
let password = 'aA1[11]';
passwordField.setValue(password);
let result = helper.validatePassword(passwordField);
expect(result).toEqual(expectedErrorResponse);
));


The output I see in the console is



password: aA1[11]
test result true









share|improve this question


























    0














    In the following code, I pass a FormControl which contains a password. I expect that when the password is aA1[11], the RegExp.test method should return false but it returns true! Why my code is returning null instead of error object
    validatePassword:
    valid: false,
    message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'



    Shouldn't this forward look up fail the match (?=.*[!@#$%^&*()_+":'?&gt.<,])



     validatePassword(control: FormControl) 

    let password: string = control.value;
    /* So the rule for password is
    6-10 length
    contains a digit
    contains a lower case alphabet
    contains an upper case alphabet
    contains one more special character from the list !@#$%^&*()_+":;'?/>.<,
    does not contain space
    */
    let REG_EXP = new RegExp('(?=^.6,10$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+":'?&gt.<,])(?!.*\s).*$');
    /*RegExp's test method returns true if it finds a match, otherwise it returns false*/
    console.log('password: ',password);
    console.log('test result ',(REG_EXP.test(password)));
    return (REG_EXP.test(password)) ? null :
    validatePassword: //check the class ShowErrorsComponent to see how validatePassword is used.
    valid: false,
    message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'





    I am calling the above function from my Karma test case



    fit('A password of length between 6-10 characters and containing at least 1 digit, at least 1 lowercase letter, at least 1 upper case ' +
    'letter and but NOT at least 1 special character from the list !@#$%^&*()_+{":;'?/>.<, shall NOT be accepted',
    inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>
    let helper = new HelperService(loaderService,httpClient);
    let passwordField = new FormControl();
    let password = 'aA1[11]';
    passwordField.setValue(password);
    let result = helper.validatePassword(passwordField);
    expect(result).toEqual(expectedErrorResponse);
    ));


    The output I see in the console is



    password: aA1[11]
    test result true









    share|improve this question
























      0












      0








      0







      In the following code, I pass a FormControl which contains a password. I expect that when the password is aA1[11], the RegExp.test method should return false but it returns true! Why my code is returning null instead of error object
      validatePassword:
      valid: false,
      message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'



      Shouldn't this forward look up fail the match (?=.*[!@#$%^&*()_+":'?&gt.<,])



       validatePassword(control: FormControl) 

      let password: string = control.value;
      /* So the rule for password is
      6-10 length
      contains a digit
      contains a lower case alphabet
      contains an upper case alphabet
      contains one more special character from the list !@#$%^&*()_+":;'?/>.<,
      does not contain space
      */
      let REG_EXP = new RegExp('(?=^.6,10$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+":'?&gt.<,])(?!.*\s).*$');
      /*RegExp's test method returns true if it finds a match, otherwise it returns false*/
      console.log('password: ',password);
      console.log('test result ',(REG_EXP.test(password)));
      return (REG_EXP.test(password)) ? null :
      validatePassword: //check the class ShowErrorsComponent to see how validatePassword is used.
      valid: false,
      message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'





      I am calling the above function from my Karma test case



      fit('A password of length between 6-10 characters and containing at least 1 digit, at least 1 lowercase letter, at least 1 upper case ' +
      'letter and but NOT at least 1 special character from the list !@#$%^&*()_+{":;'?/>.<, shall NOT be accepted',
      inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>
      let helper = new HelperService(loaderService,httpClient);
      let passwordField = new FormControl();
      let password = 'aA1[11]';
      passwordField.setValue(password);
      let result = helper.validatePassword(passwordField);
      expect(result).toEqual(expectedErrorResponse);
      ));


      The output I see in the console is



      password: aA1[11]
      test result true









      share|improve this question













      In the following code, I pass a FormControl which contains a password. I expect that when the password is aA1[11], the RegExp.test method should return false but it returns true! Why my code is returning null instead of error object
      validatePassword:
      valid: false,
      message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'



      Shouldn't this forward look up fail the match (?=.*[!@#$%^&*()_+":'?&gt.<,])



       validatePassword(control: FormControl) 

      let password: string = control.value;
      /* So the rule for password is
      6-10 length
      contains a digit
      contains a lower case alphabet
      contains an upper case alphabet
      contains one more special character from the list !@#$%^&*()_+":;'?/>.<,
      does not contain space
      */
      let REG_EXP = new RegExp('(?=^.6,10$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+":'?&gt.<,])(?!.*\s).*$');
      /*RegExp's test method returns true if it finds a match, otherwise it returns false*/
      console.log('password: ',password);
      console.log('test result ',(REG_EXP.test(password)));
      return (REG_EXP.test(password)) ? null :
      validatePassword: //check the class ShowErrorsComponent to see how validatePassword is used.
      valid: false,
      message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'





      I am calling the above function from my Karma test case



      fit('A password of length between 6-10 characters and containing at least 1 digit, at least 1 lowercase letter, at least 1 upper case ' +
      'letter and but NOT at least 1 special character from the list !@#$%^&*()_+{":;'?/>.<, shall NOT be accepted',
      inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>
      let helper = new HelperService(loaderService,httpClient);
      let passwordField = new FormControl();
      let password = 'aA1[11]';
      passwordField.setValue(password);
      let result = helper.validatePassword(passwordField);
      expect(result).toEqual(expectedErrorResponse);
      ));


      The output I see in the console is



      password: aA1[11]
      test result true






      regex angular6 karma-jasmine






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 6:14









      Manu Chadha

      2,76721338




      2,76721338






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You can use below code to validate a password-



          validatePassword(control: FormControl) 
          let password: string = control.value;

          let REG_EXP = /^(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]6,10$/i; // modify as per your requirement, currently it accept atleast 1 character,1 special character,1 [0-9] number, length between 6 to 10.

          if(!REG_EXP.test(password))
          return 'validatePassword': //check the class ShowErrorsComponent to see how validatePassword is used.
          'valid': false,
          'message': 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'









          share|improve this answer




















          • But why my code isn’t working?
            – Manu Chadha
            Nov 12 at 7:46










          • @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
            – Shailendra Singh Deol
            Nov 12 at 7:53










          • @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
            – Pushpesh Kumar Rajwanshi
            Nov 12 at 8:24











          • @ManuChadha you can update your own regex as per your requirement.
            – Shailendra Singh Deol
            Nov 12 at 12:51


















          0














          The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256757%2fregexp-test-not-giving-correct-result%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use below code to validate a password-



            validatePassword(control: FormControl) 
            let password: string = control.value;

            let REG_EXP = /^(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]6,10$/i; // modify as per your requirement, currently it accept atleast 1 character,1 special character,1 [0-9] number, length between 6 to 10.

            if(!REG_EXP.test(password))
            return 'validatePassword': //check the class ShowErrorsComponent to see how validatePassword is used.
            'valid': false,
            'message': 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'









            share|improve this answer




















            • But why my code isn’t working?
              – Manu Chadha
              Nov 12 at 7:46










            • @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
              – Shailendra Singh Deol
              Nov 12 at 7:53










            • @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
              – Pushpesh Kumar Rajwanshi
              Nov 12 at 8:24











            • @ManuChadha you can update your own regex as per your requirement.
              – Shailendra Singh Deol
              Nov 12 at 12:51















            0














            You can use below code to validate a password-



            validatePassword(control: FormControl) 
            let password: string = control.value;

            let REG_EXP = /^(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]6,10$/i; // modify as per your requirement, currently it accept atleast 1 character,1 special character,1 [0-9] number, length between 6 to 10.

            if(!REG_EXP.test(password))
            return 'validatePassword': //check the class ShowErrorsComponent to see how validatePassword is used.
            'valid': false,
            'message': 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'









            share|improve this answer




















            • But why my code isn’t working?
              – Manu Chadha
              Nov 12 at 7:46










            • @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
              – Shailendra Singh Deol
              Nov 12 at 7:53










            • @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
              – Pushpesh Kumar Rajwanshi
              Nov 12 at 8:24











            • @ManuChadha you can update your own regex as per your requirement.
              – Shailendra Singh Deol
              Nov 12 at 12:51













            0












            0








            0






            You can use below code to validate a password-



            validatePassword(control: FormControl) 
            let password: string = control.value;

            let REG_EXP = /^(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]6,10$/i; // modify as per your requirement, currently it accept atleast 1 character,1 special character,1 [0-9] number, length between 6 to 10.

            if(!REG_EXP.test(password))
            return 'validatePassword': //check the class ShowErrorsComponent to see how validatePassword is used.
            'valid': false,
            'message': 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'









            share|improve this answer












            You can use below code to validate a password-



            validatePassword(control: FormControl) 
            let password: string = control.value;

            let REG_EXP = /^(?=.*d)(?=.*[#$@!%&*?])[A-Za-zd#$@!%&*?]6,10$/i; // modify as per your requirement, currently it accept atleast 1 character,1 special character,1 [0-9] number, length between 6 to 10.

            if(!REG_EXP.test(password))
            return 'validatePassword': //check the class ShowErrorsComponent to see how validatePassword is used.
            'valid': false,
            'message': 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'










            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 7:26









            Shailendra Singh Deol

            62725




            62725











            • But why my code isn’t working?
              – Manu Chadha
              Nov 12 at 7:46










            • @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
              – Shailendra Singh Deol
              Nov 12 at 7:53










            • @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
              – Pushpesh Kumar Rajwanshi
              Nov 12 at 8:24











            • @ManuChadha you can update your own regex as per your requirement.
              – Shailendra Singh Deol
              Nov 12 at 12:51
















            • But why my code isn’t working?
              – Manu Chadha
              Nov 12 at 7:46










            • @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
              – Shailendra Singh Deol
              Nov 12 at 7:53










            • @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
              – Pushpesh Kumar Rajwanshi
              Nov 12 at 8:24











            • @ManuChadha you can update your own regex as per your requirement.
              – Shailendra Singh Deol
              Nov 12 at 12:51















            But why my code isn’t working?
            – Manu Chadha
            Nov 12 at 7:46




            But why my code isn’t working?
            – Manu Chadha
            Nov 12 at 7:46












            @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
            – Shailendra Singh Deol
            Nov 12 at 7:53




            @ManuChadha May be your regular expression is not correct. you can test your regex on regextester.com
            – Shailendra Singh Deol
            Nov 12 at 7:53












            @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
            – Pushpesh Kumar Rajwanshi
            Nov 12 at 8:24





            @Shailendra: Your answer's regex has a problem, where it doesn't individually check the presence of one lower case and one upper case character due to which it matches a1@aaa which it should not as this password doesn't contain any upper case letter. As it only checks for digit and special characters, it even allows 1@@@@@@ as a valid password. You may want to fix your regex.
            – Pushpesh Kumar Rajwanshi
            Nov 12 at 8:24













            @ManuChadha you can update your own regex as per your requirement.
            – Shailendra Singh Deol
            Nov 12 at 12:51




            @ManuChadha you can update your own regex as per your requirement.
            – Shailendra Singh Deol
            Nov 12 at 12:51













            0














            The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp






            share|improve this answer

























              0














              The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp






              share|improve this answer























                0












                0








                0






                The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp






                share|improve this answer












                The issue was with regex. I changed &amp to just & and &quot to " and &gt to > and &lt to <. It seems that in my code (angular), the letters after & were being treated literally. so the a in aA1[11] was matched with a in &amp







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 22:29









                Manu Chadha

                2,76721338




                2,76721338



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256757%2fregexp-test-not-giving-correct-result%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands