Archive for the Category » Validation «

Wednesday, December 24th, 2008 | Author: admin
string emailRegex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (!System.Text.RegularExpressions.Regex.IsMatch(email, emailRegex) && email.Length > 0)
        {
            error.Append("<li>Please enter a valid EMAIL address</li>");
            txtEmail.CssClass = "txtboxError";
        }
        else
        {
            txtEmail.CssClass = "formtextbox";
        }

Friday, October 03rd, 2008 | Author: admin

This is a very helpful article I found written by Mark Smith (link to original article).

ASP.NET 2.0 has problems when trying to validate a XHTML 1.0 Strict page using the W3 validation service.

If you are using an XHTML Strict DOCTYPE e.g.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  

Then you might notice that your ASP.NET pages don’t validate correctly when using the W3 validator service. This is because when the request comes in from the service, ASP.NET treats the service as a down level browser and the HTML that is generated doesn’t comply to the standard that the DOCTYPE needs. If you view the page from a browser and then sent the generated HTML, it would validate but this doesn’t help with direct requests from the w3 service.

To get around this problem, you need to create an ASP.NET folder in your project called “App_Browsers”. Then, inside that folder create a file named”w3cvalidator.browser” and add the following code:

<browsers>
    <!--
    Browser capability file for the w3c validator
     sample UA: "W3C_Validator/1.305.2.148 libwww-perl/5.803"
    -->
     <browser id="w3cValidator" parentID="default">
         <identification>
            <userAgent match="^W3C_Validator" />
         </identification>  

         <capture>
            <userAgent match="^W3C_Validator/(?'version'(?'major'\d+)(?'minor'\.\d+)\w*).*" />
         </capture>  

         <capabilities>
             <capability name="browser" value="w3cValidator" />
             <capability name="majorversion" value="${major}" />
             <capability name="minorversion" value="${minor}" />
             <capability name="version" value="${version}" />
             <capability name="w3cdomversion" value="1.0" />
             <capability name="xml" value="true" />
             <capability name="tagWriter" value="System.Web.UI.HtmlTextWriter" />
         </capabilities>
     </browser>
 </browsers>  

<!-- Browser capability file for the w3c validator sample UA: "W3C_Validator/1.305.2.148 libwww-perl/5.803" -->

Now, when the w3 service requests your page, it won’t be treated as a low level browser and the correct HTML will be rendered so that your page will validate (assuming you haven’t made any errors!).

 

You may also need to add the following to the web.config file

<system.web>    ... other configuration goes here ...    <xhtmlConformance mode="Strict" /></system.web>