Day 4: String Validations – II (User ID Validation)

After Password Validation, another important validation is your Login ID/ Login Name/ User ID or User Name Validation. Usually, there are not much restrictions in making a User ID except a few. That’s why I chose Password Validation topic ahead of this topic. Also, if you can make a password validator code, the same can be applied for user ID validator code also. Still, let’s make a common validator code which we mostly see. The task goes like this:

# Task 1 (Day 4): Write a validatory code to check a user ID which satisfies the following conditions:

  1. Only contains alphanumeric characters, underscore and dot.
  2. Underscore and dot can’t be next to each other (e.g abc_.def).
  3. Underscore or dot can’t be used multiple times in a row (e.g abc__def / abc..def).
  4. Underscore and dot can’t be at the end or start of a username (e.g _abc / abc_ / .abc / abc.)

This is the most general format we all see, almost in every login site. Let’s try to sort it out. What should be the approach?

  1. only contains alphanumeric characters, an underscore and a dot. This we have studied on last day. Something like this might help: [a-zA-Z0-9._]
  2. underscore or dot must not follow each other. This is something that requires a thought process. For time being, It says [ ! . _], i.e., not . _ (heights of non technicality)
  3. Step 2 will fulfill our 3rd requirement of Task as well.
  4. Using ^ anchor can solve our 4th requirement.

Let’s code it:

I have used anchor ^ to ensure that 1st letter must not be . or _. What should be the output?

What!! An error. Why?

What should be the reason? I want some alphanumeric character followed by . or _ followed by another alphanumeric character. It means that I have to write (or add) two alphanumeric characters with . or _ in between. Oh Yes!! I have to do this. ‘ | ‘ operator will help.

Oh great!! The Pipe operator is working. I had to simply add (logic only, not literally) two alphanumeric characters. Moving on to another part, how to avoid successive use of .. or __ or ._? For this, RegEx provides an important concept known as positive and negative lookahead. For detailed knowledge, go to the link. It simply says that we have to deal with negative lookahead whose syntax goes like this, a [ ? ! b ], which means a not followed by b. In our case, it will be [._] [? ! ._]. Read it as, dot underscore not followed by dot underscore. Slightly modifying the code now,

It’s working now as expected. Let’s check the code for some more combination of outputs.

Working. No two dots should be allowed.

Working. Only dot and underscore should be used. No other characters.

Working. 1st character can’t be dot or underscore. So, it’s working according to our need. Another parameter one can insert in Task 1 is length of user name. Say, I want the username to be 7 characters long but not more than 20 characters.
# Task 2 (Day 4): All that is written in Task 1 with additional parameter of length. The length must be 7 characters long but not more than 20 characters.

We have to just write {7,20} [\w] along with the running RegEx script of line 4.

Before concluding, one security check. There are some innocent people like this person

Innocent Face with Innocent Smile.

While making his User ID and Password, what he did, he used the same name and password (innocence or laziness?). Now, this is very dangerous situation. This makes the intruder’s work more easy. Let’s make a validation which prohibits this person to make User ID and Password same. Let’s help this person.

So, what I have done is that I have compared user defined Username and Password. If both are equal then a message will prompt that Username and Password cannot be the same. The 1st while loop will ask the user to enter a username and it will be matched with pattern 1. Similarly, 2nd while loop will check for password and compare it with pattern 2. Then, username and password will be compared. If they are equal, then, the user will be asked to enter a password according to pattern 2 validation part. Also, one can insert or delete more characters in their pattern according to their need. (I would like to thank stackoverflow for this, because while writing this specific code, I got stuck (it was so silly mistake by my side that i want to forget).

So, finally this way we have helped the innocent man. Let’s conclude Day 4. Thank You.

(The guy with innocent face and smile is my senior colleague. He is a proficient C and Java coder. He is the critic of my writing also making me to write better. The name is Zohaib Hasan. Recently, he has started writing non-technical issues. You can read his 1st blog here.)

Leave a comment