Validation Programs
Validation Programs
# Example 4:
def range_check(number, min_val, max_val):
if min_val <= number <= max_val:
return True
else:
return False
# Example usage:
num = 25
if range_check(num, 10, 50):
print("Number is within the range.")
else:
print("Number is outside the range.")
Length Check:
# Example 1: Checking the length of a string
user_input = input("Enter a username (between 5 and 10 characters): ")
if 5 <= len(user_input) <= 10:
print("Valid username.")
else:
print("Invalid username length.")
# Example 2: Validate the length of a string.
username = input("Enter a username: ")
if len(username) >= 5 and len(username) <= 10:
print("Valid length")
else:
print("Invalid length")
# Example 5:
def length_check(text, min_length, max_length):
if min_length <= len(text) <= max_length:
return True
else:
return False
# Example usage:
input_text = "Hello, World!"
if length_check(input_text, 5, 15):
print("Text length is valid.")
else:
print("Text length is invalid.")
Presence Check:
# Example 1: Checking if a string is not empty
user_input = input("Enter your name: ")
if user_input:
print(f"Hello, {user_input}!")
else:
print("Name is required.")
# Example 4:
def presence_check(text):
if text:
return True
else:
return False
# Example usage:
input_text = ""
if presence_check(input_text):
print("Text is present.")
else:
print("Text is absent.")
Type Check:
# Example 1: Checking if the input is an integer
user_input = input("Enter an integer: ")
if user_input.isdigit():
user_input = int(user_input)
print(f"Valid integer: {user_input}")
else:
print("Invalid input. Please enter an integer.")
# Example 4:
def type_check(value, data_type):
if isinstance(value, data_type):
return True
else:
return False
# Example usage:
num = 42
if type_check(num, int):
print("Value is an integer.")
else:
print("Value is not an integer.")
Format Check:
# Example 1: Checking if an email address has a valid format
import re
# Example 3:
import re
def format_check(email):
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
if re.match(pattern, email):
return True
else:
return False
# Example usage:
email_address = "[email protected]"
if format_check(email_address):
print("Email format is valid.")
else:
print("Email format is invalid.")
total = 0
for i in range(12):
total += int(isbn[i]) * (1 if i % 2 == 0 else 3)