How to Validate User Account Details Using Regular Expressions

Regular expressions are an integral programming tool that you can use for many practical day-to-day tasks. You can use regular expressions to search, match, or parse text. With the right patterns, you can validate some of the most common user account details.

you’re able to use regular expressions with many languages or tools, including Python and JavaScript.

4

Regex to Validate a Username

Consider a username that is only valid if it satisfies the following conditions:

The code used in this project is available in aGitHub repositoryand is free for you to use under the MIT license.

regular expressions code

The following regex satisfies the above conditions and can validate a username:

If you are not comfortable with the above expression, check outa beginner’s guide to regular expressionsfirst. This is a Python approach to validate a username:

username regex output

Running this code will confirm that the first username is valid but the second isn’t:

Similarly, you can validate a username in JavaScript using the following code:

Email address structure

You can use this code tovalidate HTML forms using regular expressions.

Regex to Validate an Email Address

The regex to validate an email address is not perfect. There’s no universally agreed-upon regex to validate an email address. It completely boils down to your definition of valid.

Below is a list of conditions that can validate most email addresses:

email id regex output

The following regex satisfies the above conditions and can validate an email address:

This is a Python approach to validating an email address:

Again, the output confirms the first email address is valid while the second is invalid:

You can validate an email in JavaScript using the following code:

Check Password Strength Using Regular Expressions

Strong passwords are essential from a security perspective. You need to make sure that end users have strong enough passwords so that others cannot access their accounts.

The following rules check that that the password strength of your application is strong:

The following regex satisfies the above conditions and can help ensure a stronger password:

You can check password strength in Python using the following code:

Running this code will confirm that the first password is strong while the second one is weak:

you may check the password strength in JavaScript using the following code:

Regular Expression to Match a Valid Date

If you want to quickly check whether the given dates are in the traditional date format or not, you’re able to do so using regex.

The following regular expression matches a date inmm/dd/yyyyformat:

The regex for the date has some limitations, it doesn’t validate dates such as February 31st. It only validates whether the given string looks like a date or not.

The following Python code validates the date is inmm/dd/yyyyformat:

Once more, the output confirms the first date format is valid, but the second is invalid:

You can validate the date inmm/dd/yyyyformat in JavaScript using the following code:

Empty String Validation Using Regular Expressions

You can check if a string is empty using the following regular expression:

Below is the Python approach for empty string validation:

The following output demonstrates that the first given string is empty while the second isn’t:

Use the following JavaScript code to check if the string is empty or not:

RegEx to Validate ZIP Code (US Postal Code)

you could validate a ZIP code (U.S. Postal Code) in both the five-digit and nine-digit (called ZIP+4) formats using the following regular expression:

Below is the Python code to validate ZIP codes:

Running this code will confirm that the first and second ZIP codes are valid but the third isn’t:

Use the following JavaScript code to validate the ZIP code using regex:

Verify User Input With Robust Code

You learned how to validate user account details using regular expressions. Validating these details makes the code robust and helps to tackle security issues and unwanted bugs. Robust code provides a safe and secure experience for your users.

You should make sure you are validating the input data either on the client side or the server side to be always on the safe side from hackers.

Take user input, manipulate data, and update a page accordingly. All with HTML and JavaScript.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

OneDrive is one of the best, but it has a catch.

The best features aren’t the ones being advertised.

Lose your laptop without this feature, and you’ll wish you had turned it on.

Technology Explained

PC & Mobile