Password Validation

Hello everyone! I’m a beginner in coding and I’m trying to solve this task. I know I need to use RegEx but still not sure how to make the code. Pls help!

Any password must include ‘n’ and ‘c’ (upper or lowercase) and must end with ‘1’.
You need to design this function to check the password it is passed. If it fits all of the above criteria then you should return ‘valid’ else return ‘invalid’.

function passwordValidation(password) {


You can use this to help you create a regex pattern
You can use this to help you how to implement it Regular expressions - JavaScript | MDN

1 Like

Ok, so I deleted my last posts because I found that “solution” had some edge cases that were not accounted for. This regex should work in your case, but let me know if you find any cases that maybe I did not find.

This regex can also be replaced for with a loop with some conditional logic. I personally have not had a use for regex in a while, so I thought this might be a good thing to do this as a refresher for myself, as well as help someone else…plus it is just fun.


  • non-case sensitive alpha characters can be used
  • numeric characters can be used
  • special characters such as space, newline, tabs, !..etc, can not be used in string
  • string must have n and c characters included, in any order.
  • string must end in 1
  • will work on string with indefinite length.

Alternately a positive lookahead can make your regex at bit less verbose, accounting simply for the requirements as noted above.


That would be another way.

Since @Dimitrina mentioned this is for passwords I did not include wildcards, since that would include ANY character such as spaces, that you typically would not find in passwords.

Also, (?=.*(N|n))(?=.*(C|c)).+1 would need to be anchored to at least the end of the string, for example the string n cc a n1ncnc would still find a match even with 1 not being at the end of the string and with none alpha or numeric values being used.

Fair point, however I would argue that there is no need to introduce further complexity when the requirements are fairly straightforward and its simply a beginner exercise.

Regarding, the anchor, you are correct, I forgot to include that and should be as so:


I prefer to just not argue at all haha. I would say do it by any means you can if you are learning regular expressions.

I would not think either example would be under the category of beginner example. You need to know about classes, alternation, anchors, lookarounds, quantifiers…etc.

Either way, @Dimitrina doesn’t seem to be active on the post, so I am thinking they have applied a solution and moved on. If anyone else reads this they now have examples of strict and loose applications.