Form Validation Techniques: Don Edlin, MRSC
Form Validation Techniques: Don Edlin, MRSC
Techniques
1
Why Validate?
Usability
Frustrating for the user
Data Integrity
Ensure your getting data in the format you expect
Security
Keeping your forms from being used against you or your users
Assume all input is evil
2
SQL Injection Attacks
Principle Implementation
Never trust user input Validate all textbox entries using validation
controls, regular expressions, code, and so on
3
Cross Site Scripting Attack
4
Approaches to Form Validation
5
Where to Validate
Server Side
CGI, ASP, .Net, Coldfusion, etc.
Pros
Ensures that every time the form is submitted, the validation will
run.
Allows for validation against other server resources, such as a
backend database and business rules.
Cons
Puts more load on the server.
Slower
6
Where to Validate
Client Side
JavaScript
Pros
7
The Answer
Most likely you will want a combination of client side and server side
validation.*
Consider JavaScript validation as adding usability, not providing security.
Never use client side validation for security.
Do include a maximum length on your input tags.
Encode input from users and querystrings.
*Some validation will run on both the client and on the server.
.Net validation controls for example
8
When Server Side is also Client Side
Many server side controls will produce JavaScript for validation
when the page is served.
Asp.net form field with validation
<form runat="server">
Email:<asp:textbox id="txtEmail" size="20" runat="server"/><br>
<ASP:RequiredFieldValidator ControlToValidate="txtEmail" Display="Static"
ErrorMessage="*Email is a required field."
runat="server" />
9
JavaScript for Usability
10
JavaScript Zip Code Example
function validateZIP(field) {
var valid = "0123456789-";
var hyphencount = 0;
12
Some of The Special Characters
13
Example
14
Regular Expression Code
re = /(^\d{5}$)|(^\d{5}-\d{4}$)/
function validateZIP(field) {
if (re.test(field)) {
return true
}
alert("Invalid Zip Code")
return false }
15
Regular Expression Code
/(^\d{5}$)|(^\d{5}-\d{4}$)/
/ - start and end of regular expression
() - used for grouping
\d – matches any digit 0 through 9
{5} – at least 5 occurrences of the previous character
$ - end of string
| - alternation - or
16
Conclusion
17
Resources
Security
http://www.owasp.org/index.jsp
http://www.cgisecurity.com/articles/xss-faq.shtml#whatis
http://www.technicalinfo.net/papers/CSS.html
http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/default.aspx
18