yyyZ
Culture
Theme
Menu
8/20/2008 9:52:27 AM
Contact Us
Wed, 20 Aug 2008 10:51 am CDT
Chicago, IL 60610
Partly Cloudy
Temperature 76ºF
Feels Like 76ºF
Wind SE at 6 Mph
Visibility 0.1 Miles
Humidity 45%
Sunrise 6:04 am
Sunset 7:42 pm
Hi:82ºF/Lo:64ºF

Tomorrow
Mostly Cloudy
Hi:84ºF/Lo:70ºF

Yahoo! Weather

Password Control

Password Control - Enter a Password from 8 to 12 charactes in length with a minimum of 3 characters and 2 digits. The password entered will be validated at the server when you press the button. The code for the password control apears below.
Password:
 
Minimum Length 
Maximum Length 
Number of Characters 
Number of Digits 
Character Case 


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Gfw.Web.Validators
{
    /// <summary>
    /// Summary description for PasswordValidator.
    /// <usage>
    /// To Include it in an aspx page :
    /// <!--<%@ Register TagPrefix="yZVp" Namespace="Gfw.Web.Validators"
        Assembly="Gfw.Web" %>-->
    /// <!--<yzLv:PasswordValidator 
    /// ID="yzLvId" 
    /// ControlToValidate="txtBox" 
    /// RUNAT="SERVER" 
    /// Minimum="8" 
    /// Maximum="12" 
    /// Chars="3" 
    /// Digits="2">
    /// Invalid
    /// </yzLV:PasswordValidator>-->
    /// </usage>
    /// </summary>
    public class PasswordValidator : BaseValidator 
    {
        private int    m_Minimum     = 1;  // Minimum Length
        private int    m_Maximum     = 80; // Maximum Length 
        private int    m_Digits      = 0;  // Min # of Digits
        private int    m_Chars       = 0;  // Min # of Chars 
        private int    m_CountDigits = 0;  // Number of Digits in Password 
        private int    m_CountChars  = 0;  // Number of Chars in Password
        private int    m_MixCase     = 0;  // 1=Use Mixed Case 
        private bool   m_IsUpper     = false; // Are there Upper Case
        private bool   m_IsLower     = false; // Are there Lower Case 
        private string m_errMessage;

        /// <summary>Minimum Length</summary>
        public int Minimum  {set {m_Minimum  = value; }    
        } 
        /// <summary>Maximum Length</summary>
        public int Maximum  {set {m_Maximum  = value; }    
        }
        /// <summary>Number of Digits</summary>
        public int Digits    {set {m_Digits   = value; }    
        }
        /// <summary>Number of Chars</summary>
        public int Chars    {set {m_Chars    = value; }    
        }
        /// <summary>Use Mixed Case</summary>
        public int Mixed    {set {m_MixCase  = value; }    
        }

        // Count Number of Digits
        private void CountDigits(char ch)
        {
            if(ch>='0' && ch<='9') ++m_CountDigits;
        }
        // Count Number of Chars
        private void isChar(char ch)
        {
            if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) ++m_CountChars;
        }
        // Check for Mixed Case
        private void isMixed(char ch)
        {
            if(ch>='a' && ch<='z') m_IsLower=true;
            if(ch>='A' && ch<='Z') m_IsUpper=true;
        }
    
        /// <summary>Override EvaluateIsValid</summary>
        protected override bool EvaluateIsValid() 
        {
            string vCtrl = this.GetControlValidationValue(this.ControlToValidate);
            m_errMessage = string.Empty;

            // Check Minimum and Maximum Length of Password
            bool lOk = false; //
                Length
            if (vCtrl.Length>=m_Minimum && vCtrl.Length<=m_Maximum)    lOk= true;
            if (vCtrl.Length<m_Minimum) m_errMessage 
			= "Please Enter at least "+m_Minimum.ToString()+"
                Characters";
            if (vCtrl.Length>m_Maximum) m_errMessage 
			= "Please Enter no more than "+m_Maximum.ToString()+" Characters";

            // Look for Minimum Number of Characters
            bool cOk = false; 
            for(int i=0;i< vCtrl.Length;i++) isChar(vCtrl[i]);
            if (m_CountChars>=m_Chars) cOk=true;
            if (m_CountChars< m_Chars && m_Chars>0) m_errMessage 
			= "Please Enter at least "+m_Chars.ToString()+"
                Alpha [a-Z] Characters";

            // Look for Minimum Number of Digits
            bool dOk = false; 
            for(int i=0;i< vCtrl.Length;i++) CountDigits(vCtrl[i]);
            if (m_CountDigits>=m_Digits) dOk=true;
            if (m_CountDigits< m_Digits && m_Digits>0) m_errMessage 
			= "Please Enter at least "+m_Digits.ToString()+"
                [0-9] Digits";

            // Look for Mixed Case
            bool mOk = false; 
            for(int i=0;i< vCtrl.Length;i++) isMixed(vCtrl[i]);
            if (m_IsUpper && m_IsLower || m_MixCase==0) mOk=true;
            else m_errMessage = "Please Use Upper AND
                Lower Case Characters";

            this.ErrorMessage = m_errMessage;

            if(lOk && cOk && dOk && mOk) return true;
            return false;
        }
    } 
}