11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

266 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sNamesThe following example shows a RegularExpressionValidator control that has beenused to validate a name field.The preceding validation expression constrains the input name field to alphabeticcharacters (lowercase <strong>and</strong> uppercase), the single apostrophe for names such asO’Dell, <strong>and</strong> the dot character. In addition, the field length is constrained to 40characters.Social <strong>Security</strong> NumbersThe following example shows the HTML code that is generated for aRegularExpressionValidator control that has been used to validate a U.S. socialsecurity number form field:The preceding validation expression is one of the st<strong>and</strong>ard expressions that VisualStudio .NET provides. It validates the format of the supplied input field as well as itstype <strong>and</strong> length. The input must consist of three numeric digits followed by a dash,then two digits followed by a dash, <strong>and</strong> then four digits.If you are not using server controls (which rule out the validator controls), or youneed to validate input from sources other than form fields, you can use theSystem.Text.RegularExpression.Regex class in your method code. The followingexample shows how to validate the same field by using the static Regex.IsMatchmethod directly in the page class rather than using a validator control:if (!Regex.IsMatch(txtSSN.Text, @"\d{3}-\d{2}-\d{4}")){// Invalid Social <strong>Security</strong> Number}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!