Click here to Skip to main content
15,895,667 members
Articles / Web Development / ASP.NET

Single Control Validation Solution

Rate me:
Please Sign up or sign in to vote.
4.78/5 (6 votes)
17 Sep 2002CPOL5 min read 184.8K   56  
A single control to validate a .NET Web Form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Validator
{
    public partial class RegisterForm : Form
    {

        public string Information
        {
            get
            {
                StringBuilder builder = new StringBuilder();
                if (!string.IsNullOrEmpty(txtName.Text))
                {
                    builder.AppendLine(string.Format("Name: {0}", this.txtName.Text));
                }
                if (!string.IsNullOrEmpty(txtPassword.Text))
                {
                    builder.AppendLine(string.Format("Password: {0}", this.txtPassword.Text));
                }
                if (!string.IsNullOrEmpty(txtBirth.Text))
                {
                    builder.AppendLine(string.Format("Birth: {0}", this.txtBirth.Text));
                }
                if (!string.IsNullOrEmpty(txtIncome.Text))
                {
                    builder.AppendLine(string.Format("Income: {0}", this.txtIncome.Text));
                }
                if (!string.IsNullOrEmpty(txtEmail.Text))
                {
                    builder.AppendLine(string.Format("Email: {0}", this.txtEmail.Text));
                }

                return builder.ToString();
            }
        }

        public RegisterForm()
        {
            InitializeComponent();
        }

        private void RegisterForm_Load(object sender, EventArgs e)
        {
            Random random = new Random();
            int value = random.Next(1000, 9999);
            lblCode.Text = value.ToString();
        }

        private void validator1_CustomValidation(object sender, Itboy.Components.ValidationEventArgs e)
        {
            if (e.Control == txtCode)
            {
                if (txtCode.Text == lblCode.Text)
                {
                    e.Valid = true;
                }
                else
                {
                    e.Valid = false;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
I'm a Software Architect, working for a telecom in Portland, OR. My specialties are C#, Win32 C/C++ programming, Visual Basic (6 and .NET), and ASP(.NET), XML/XSL, and database programming. I'm currently learning F#. Completely different than anything I've ever done, but very cool. I'm married to the most wonderful woman, have a beautiful daughter that I'm very proud of, and a step-son that's rocking in high school. I'm also a 2nd degree blackbelt in Olympic style (WTF) Taekwondo, and have trained in Brazilian Jiujitsu, Jeet Kun Do, Krav Maga.

Comments and Discussions