DEV Community

Free Python Code
Free Python Code

Posted on

Examples of how to use the validators module in Python

Hi 🙂🖐

We know that validators are very important; they're used in many things like email, usernames, and passwords. In this post, I will share with you how to make validation by using a module called validators.

First, you need to install validators

pip install validators

[1] - URL Validation

import validators

print(validators.url('https://dev.to')) # True
Enter fullscreen mode Exit fullscreen mode

validators.url returns true' if the URL is valid. If not validated, it will return ValidationError.

import validators

print(validators.url('https//dev.to')) 
Enter fullscreen mode Exit fullscreen mode

result

ValidationError(func=url, args={'value': 'https//dev.to'})
Enter fullscreen mode Exit fullscreen mode

[2] - Email Validation

import validators

print(validators.email('test@test.com')) # True
Enter fullscreen mode Exit fullscreen mode

validators.email returns true' if Email is valid. If not validated, it will return ValidationError.

[3] - Domain Validation

import validators

print(validators.domain('test.com')) # True
Enter fullscreen mode Exit fullscreen mode

[4] - Card Numbers Validation

You can find Card Numbers to test from here : https://support.bluesnap.com/docs/test-credit-card-numbers

import validators

print(validators.card_number('6250941006528599')) # True
Enter fullscreen mode Exit fullscreen mode

You can choose the card type that you want to validate. You have many types, like visa, Mastercard, Amex

Validate Visa card number

import validators

print(validators.visa('4263982640269299')) # True
Enter fullscreen mode Exit fullscreen mode

Validate mastercard number

import validators

print(validators.mastercard('5425233430109903')) # True
Enter fullscreen mode Exit fullscreen mode

Validate amex number


import validators

print(validators.amex('374245455400126')) # True
Enter fullscreen mode Exit fullscreen mode

Now I will let you discover more. What validators can validate 🤗

Now we're done 🤗

Don't forget to like and follow 🙂

Support me on PayPal 🤗
https://www.paypal.com/paypalme/amr396

Top comments (0)