Listing 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace AjaxValidation
{
    [DisplayName("AJAX Field Validation")]
    public class AjaxValidationRule: ValidationRule
    {
        private string _control = "";
	private string _value = "";

        [Description("Control To Validate"), DisplayName("Control Name")]
        public string ControlName
        {
            get { return _control; }
            set { _control = value; }
        }
        

        [Description("Value of the control to validate."), DisplayName("Value")]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }

        public override void Validate(object sender, ValidationEventArgs e)
        {
            Regex r = new Regex("(?<field>id=\"" + _control + "\".*>)(?<value>.*)<");
            MatchCollection m = r.Matches(e.Response.BodyString);
            for (int i = 0; i < m.Count; i++)
            {
                if (m[i].Groups["value"] != null)
                {
                    string foundValue = m[i].Groups["value"].Captures[0].Value;
                     e.IsValid = (foundValue == _value);
                    break;
                }
            }
        }
    }
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.