What is input validation?

In short
Input validation is the practice of checking that data meets an application's rules before the application uses it. It helps stop malformed data, invalid operations and excessive resource use. Good validation runs on the server, rejects anything outside the rules, and works alongside other defenses like parameterized queries.
What input validation means
Every application receives data from somewhere: a web form, an API call, a file upload, or another service. Input validation is the step where the application checks that this data matches what it expects before it uses the data. If a value fails the check, the application rejects it instead of processing it.
The goal is to reduce three kinds of trouble. The first is malformed data, such as a date written in the wrong format. The second is invalid business operations, such as a booking that ends before it starts. The third is excessive resource use, such as a request with a huge list that eats up memory.
Validation is only one part of security. The OWASP Cheat Sheet, which is the source for this page, makes a clear point here. Checking data does not replace the other defenses you need when you use it. For example, a database query still needs parameterized queries, and output shown on a web page still needs encoding to prevent cross-site scripting (XSS). Authorization is also separate. A valid account ID does not mean the current user is allowed to open that account.
How it works
Good validation checks two things. The first is syntax, which means the expected type and format. A quantity should be a whole number, and an email address should look like an email address. The second is semantics, which means whether the value makes sense for the task. A booking needs valid dates, and its end date must come after its start date. Checking the type alone is not enough. Turning text into an integer tells you the value is a number, but not whether that number is an acceptable quantity.
You define rules for each field. The rules depend on the kind of field:
- Fixed choices: the value must be one of an allowed set, including values from a drop-down menu.
- Numbers and dates: the expected type, an accepted format, and minimum and maximum values.
- Strings: length limits and the characters or structure the field needs.
- Objects: which fields are allowed, which are required, and how missing or null values are handled.
- Arrays: minimum and maximum item counts, and a check on every item, including nested objects.
The rules should also describe what happens when a value fails. Reject the request with a clear error. Do not continue with a partly checked request.
Allowlists and denylists
There are two broad ways to write rules. A denylist tries to list bad values and block them. An allowlist lists good values and rejects everything else. The OWASP guidance favors the allowlist. Trying to recognize every malicious string is a losing game, because attackers can always find new forms.
A denylist can also break legitimate input. Blocking apostrophes, for example, rejects real names such as O'Brien, and it still does not make a database query safe. For a free-text comment field, an allowlist can accept a wide range of characters and limit only the length. It does not need to restrict users to letters and digits.
A simple example
Imagine a booking form that accepts a start date, an end date and a number of guests. A solid server-side check would confirm that both dates parse as real dates, that the end date is after the start date, and that the guest count is a whole number between 1 and a limit you choose. If any check fails, the server returns an error and does not save the booking.
start_date = parse_date(request.start)
end_date = parse_date(request.end)
if start_date is None or end_date is None:
reject("Dates must be valid")
if end_date <= start_date:
reject("End date must be after start date")
guests = parse_int(request.guests)
if guests is None or guests < 1 or guests > 10:
reject("Guests must be between 1 and 10")
Notice that the first check confirms the data type and the second checks whether the values make sense together. Both are needed.
Common mistakes
The first mistake is relying on client-side checks alone. A browser can check fields to give fast feedback, but anyone can bypass a browser check. The server must validate the same rules again.
The second mistake is trusting data that comes from inside your system. Data from internal APIs, partner feeds, message queues or stored records should be validated when it crosses a trust boundary. An internal connection does not guarantee that a value meets the receiving component's rules.
The third mistake is cleaning input by removing suspicious characters. Removing characters can change the meaning of the data, and it does not replace parameterized queries or output encoding.
The fourth mistake is testing only malformed syntax. Test values at the edges of accepted ranges, missing fields, invalid nested items and oversized lists too.
Finally, be careful what you log. Record that a request was rejected and the relevant details, but avoid storing secrets or full request bodies in logs. If you keep untrusted values in logs, escape them so attackers cannot forge log lines.
Where to go next
Input validation is one of the first controls a developer should learn, because most web attacks begin with data that the application did not expect. Start with the rules for your own fields, write the server-side checks, and then add the other protections for each place the data is used. The OWASP Input Validation Cheat Sheet covers many more topics, including regular expressions, file uploads and Host headers, and it is a good next read once the basics feel comfortable.
FAQ
Is input validation the same as sanitization?
Not quite. Validation checks whether data meets the rules and rejects it if it does not. Sanitization changes the data to make it safe. The OWASP guidance prefers validation with clear rules, because cleaning input can change its meaning.
Should I validate on the client or the server?
Do both, but the server check is the one that counts. Client checks give users quick feedback, while server checks protect the application, because a browser check can always be bypassed.
Does validating input stop SQL injection?
Validation alone does not. You also need parameterized queries, which keep data separate from the SQL command. Validation helps by rejecting values that cannot be correct, but it is not enough on its own.
Can I use regular expressions to validate everything?
Regular expressions work well for simple, structured fields such as a postal code format. They should match the whole value, have length limits, and avoid patterns that can slow down badly. They are not a fit for complex rules or rich content.
Sources
This page is based on the official documentation below. Read it for full detail.
Input validation — official documentation ↗