Django

Create an Age Validation Form that Checks Eligibility (Age โ‰ฅ 18)

๐Ÿ“Œ 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.py to define the age field
  • views.py to apply eligibility logic
  • template to 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

  • IntegerField is used because age is a whole number
  • min_value=0 prevents 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

  1. User opens the form page
  2. Enters age
  3. Form is submitted using POST
  4. Django validates the input
  5. View applies age-based condition
  6. 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

  1. Change eligibility age from 18 to 21
  2. Show different messages for child, teen, and adult
  3. Add name field and display personalized result
  4. Prevent unrealistic ages like above 120
  5. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *