๐ Introduction
Many real-world applications need age-based validation, such as:
- voting eligibility
- admission eligibility
- registration forms
In this program, we will:
- accept age from the user
- validate the input
- check whether the user is eligible or not
- display the result on the same page
๐ Eligibility Rule:
- If age โฅ 18 โ Eligible
- Else โ Not Eligible
๐ฏ Program Statement
๐ Create an age validation form that checks eligibility (age โฅ 18).
๐ง Concept
This program uses:
forms.pyto define the age fieldviews.pyto apply eligibility logictemplateto show form and result
โ๏ธ Step 1: Create Form
๐ File: forms.py
๐น Path:
myproject/myapp/forms.py
๐น Code:
from django import forms
class AgeForm(forms.Form):
age = forms.IntegerField(label='Enter Your Age', min_value=0)
๐ง Explanation
IntegerFieldis used because age is a whole numbermin_value=0prevents negative ages
โ๏ธ Step 2: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
from .forms import AgeForm
def age_validation(request):
result = None
age_value = None
if request.method == 'POST':
form = AgeForm(request.POST)
if form.is_valid():
age_value = form.cleaned_data['age']
if age_value >= 18:
result = "Eligible"
else:
result = "Not Eligible"
else:
form = AgeForm()
return render(request, 'age_validation.html', {
'form': form,
'age': age_value,
'result': result
})
๐ง Explanation
- Age is collected using
cleaned_data - View checks whether age is 18 or more
- Result is passed to the template
โ๏ธ Step 3: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('age-validation/', views.age_validation, name='age_validation'),
]
โ๏ธ Step 4: Create Template
๐ File: age_validation.html
๐น Path:
myproject/templates/age_validation.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Age Validation Form</title>
</head>
<body>
<h1>Age Validation Form</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Check Eligibility</button>
</form>
<hr>
{% if result is not None %}
<h2>Entered Age: {{ age }}</h2>
<h2>Status: {{ result }}</h2>
{% endif %}
</body>
</html>
๐ง Explanation
- Form is displayed using
{{ form.as_p }} - Result is shown only after form submission
- Both entered age and eligibility status are displayed
โ๏ธ Step 5: Run Server
python manage.py runserver
๐ Step 6: Output
Open:
๐ http://127.0.0.1:8000/age-validation/
โ Example 1:
Input:
- Age = 20
Output:
Entered Age: 20
Status: Eligible
โ Example 2:
Input:
- Age = 15
Output:
Entered Age: 15
Status: Not Eligible
๐ง How It Works
- User opens the form page
- Enters age
- Form is submitted using POST
- Django validates the input
- View applies age-based condition
- Template displays eligibility result
๐ฅ Key Concepts
IntegerField
forms.IntegerField()
Used for whole number input.
Minimum Value Validation
min_value=0
Prevents invalid negative age.
Eligibility Condition
if age_value >= 18:
Checks whether the user is eligible.
Conditional Output
{% if result is not None %}
Displays output only after submission.
โ ๏ธ Common Errors
โ Using CharField instead of IntegerField
Age should be numeric, so IntegerField is correct.
โ Negative values allowed
Use:
min_value=0
to prevent invalid age input.
โ Forgot {% csrf_token %}
POST form will fail due to CSRF verification error.
โ Result not showing properly
Use:
{% if result is not None %}
instead of {% if result %}.
โ Wrong comparison operator
Use:
if age_value >= 18
for correct eligibility logic.
๐งช Practice Questions
- Change eligibility age from 18 to 21
- Show different messages for child, teen, and adult
- Add name field and display personalized result
- Prevent unrealistic ages like above 120
- Display result in green/red color
๐ค Viva Questions & Answers
1. Why is IntegerField used for age?
IntegerField is used because age is generally represented as a whole number. It also ensures only numeric input is accepted.
2. What is the purpose of min_value=0?
It prevents users from entering negative age values, which are invalid in real life.
3. Why is POST method used in this form?
POST method is used because the user is submitting data to the server for validation and processing.
4. What is cleaned_data used for?
cleaned_data is used to access validated input safely after is_valid() returns true.
5. Why do we check the condition in views.py instead of template?
Business logic and processing should be done in the view, while the template should only display the output.
6. What happens if the user enters text instead of a number?
Django form validation will reject the input and display an error message.
๐ Next Post: Create a Form Containing Dropdown, Checkbox, and Radio Button Inputs
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
