Click here to Skip to main content
15,888,967 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i want to take all values like length and min length , range of string form form textbox, plz tell how

001.namespace ssp.PwdGen
002.{
003.  using System;
004.  using System.Security.Cryptography;
005.  using System.Text;
006. 
007.  public class PasswordGenerator
008.  {
009.    public PasswordGenerator()
010.    {
011.      this.Minimum         = DefaultMinimum;
012.      this.Maximum         = DefaultMaximum;
013.      this.ConsecutiveCharacters = false;
014.      this.RepeatCharacters    = true;
015.      this.ExcludeSymbols      = false;
016.      this.Exclusions        = null;
017. 
018.      rng = new RNGCryptoServiceProvider();
019.    }    
020.     
021.    protected int GetCryptographicRandomNumber(int lBound, int uBound)
022.    {  
023.      // Assumes lBound >= 0 && lBound < uBound
024. 
025.      // returns an int >= lBound and < uBound
026. 
027.      uint urndnum;  
028.      byte[] rndnum = new Byte[4];  
029.      if (lBound == uBound-1) 
030.      {
031.        // test for degenerate case where only lBound can be returned
032. 
033.        return lBound;
034.      }
035.                                 
036.      uint xcludeRndBase = (uint.MaxValue -
037.        (uint.MaxValue%(uint)(uBound-lBound)));  
038.       
039.      do
040.      {   
041.        rng.GetBytes(rndnum);   
042.        urndnum = System.BitConverter.ToUInt32(rndnum,0);   
043.      } while (urndnum >= xcludeRndBase);  
044.       
045.      return (int)(urndnum % (uBound-lBound)) + lBound;
046.    }
047. 
048.    protected char GetRandomCharacter()
049.    {       
050.      int upperBound = pwdCharArray.GetUpperBound(0);
051. 
052.      if ( true == this.ExcludeSymbols )
053.      {
054.        upperBound = PasswordGenerator.UBoundDigit;
055.      }
056. 
057.      int randomCharPosition = GetCryptographicRandomNumber(
058.        pwdCharArray.GetLowerBound(0), upperBound);
059. 
060.      char randomChar = pwdCharArray[randomCharPosition];
061. 
062.      return randomChar;
063.    }
064.     
065.    public string Generate()
066.    {
067.      // Pick random length between minimum and maximum  
068. 
069.      int pwdLength = GetCryptographicRandomNumber(this.Minimum,
070.        this.Maximum);
071. 
072.      StringBuilder pwdBuffer = new StringBuilder();
073.      pwdBuffer.Capacity = this.Maximum;
074. 
075.      // Generate random characters
076. 
077.      char lastCharacter, nextCharacter;
078. 
079.      // Initial dummy character flag
080. 
081.      lastCharacter = nextCharacter = '\n';
082. 
083.      for ( int i = 0; i < pwdLength; i++ )
084.      {
085.        nextCharacter = GetRandomCharacter();
086. 
087.        if ( false == this.ConsecutiveCharacters )
088.        {
089.          while ( lastCharacter == nextCharacter )
090.          {
091.            nextCharacter = GetRandomCharacter();
092.          }
093.        }
094. 
095.        if ( false == this.RepeatCharacters )
096.        {
097.          string temp = pwdBuffer.ToString();
098.          int duplicateIndex = temp.IndexOf(nextCharacter);
099.          while ( -1 != duplicateIndex )
100.          {
101.            nextCharacter = GetRandomCharacter();
102.            duplicateIndex = temp.IndexOf(nextCharacter);
103.          }
104.        }
105. 
106.        if ( ( null != this.Exclusions ) )
107.        {
108.          while ( -1 != this.Exclusions.IndexOf(nextCharacter) )
109.          {
110.            nextCharacter = GetRandomCharacter();
111.          }
112.        }
113. 
114.        pwdBuffer.Append(nextCharacter);
115.        lastCharacter = nextCharacter;
116.      }
117. 
118.      if ( null != pwdBuffer )
119.      {
120.        return pwdBuffer.ToString();
121.      }
122.      else
123.      {
124.        return String.Empty;
125.      }  
126.    }
127.       
128.    public string Exclusions
129.    {
130.      get { return this.exclusionSet;  }
131.      set { this.exclusionSet = value; }
132.    }
133. 
134.    public int Minimum
135.    {
136.      get { return this.minSize; }
137.      set
138.      {
139.        this.minSize = value;
140.        if ( PasswordGenerator.DefaultMinimum > this.minSize )
141.        {
142.          this.minSize = PasswordGenerator.DefaultMinimum;
143.        }
144.      }
145.    }
146. 
147.    public int Maximum
148.    {
149.      get { return this.maxSize; }
150.      set
151.      {
152.        this.maxSize = value;
153.        if ( this.minSize >= this.maxSize )
154.        {
155.          this.maxSize = PasswordGenerator.DefaultMaximum;
156.        }
157.      }
158.    }
159. 
160.    public bool ExcludeSymbols
161.    {
162.      get { return this.hasSymbols; }
163.      set { this.hasSymbols = value;}
164.    }
165. 
166.    public bool RepeatCharacters
167.    {
168.      get { return this.hasRepeating; }
169.      set { this.hasRepeating = value;}
170.    }
171. 
172.    public bool ConsecutiveCharacters
173.    {
174.      get { return this.hasConsecutive; }
175.      set { this.hasConsecutive = value;}
176.    }
177. 
178.    private const int DefaultMinimum = 6;
179.    private const int DefaultMaximum = 10;
180.    private const int UBoundDigit    = 61;
181. 
182.    private RNGCryptoServiceProvider    rng;
183.    private int       minSize;
184.    private int       maxSize;
185.    private bool        hasRepeating;
186.    private bool        hasConsecutive;
187.    private bool        hasSymbols;
188.    private string      exclusionSet;
189.    private char[] pwdCharArray = "abcdefghijklmnopqrstuvwxyzABCDEFG" +
190.      "HIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()-_=+[]{}\\|;:'         \",<" +
      ".>/?".ToCharArray();                     
  }
}
Posted
Updated 6-Mar-11 18:40pm
v2
Comments
Prerak Patel 7-Mar-11 0:42am    
what is the problem?
situ21 7-Mar-11 1:12am    
i want to get all values from texbox which is in form. platform is c#.net
Prerak Patel 7-Mar-11 1:16am    
Sorry but still it is not clear. I think you have to work on your question so that anyone here can get you good solution for that.
situ21 7-Mar-11 1:49am    
i use this code in c#.net and I want to get all data from textbox like maxvalue ,minimum value, range.
Sergey Alexandrovich Kryukov 7-Mar-11 0:57am    
And what is minlength for a text box? a range of string -- what is it?
--SA

1 solution

"I want to get all data from textbox like maxvalue ,minimum value, range."
I think what you are asking is to get a range of values from a single textbox?

That is not normal. A text box should contain one value: the user enters that, and then moves to the next. When all are completed, you can then access each individual one, check it, and if all are valid, use them.

int maxValue = 0;
if (int.TryParse(inputMaxValue.Text, out maxValue))
   {
   if (maxValue < 9999)
      {
      // Process.
      }
   else
      {
      // Error: max Value out of range
      }
   }
else
   {
   // Error: max value must be a number
   }
Then repeat for minimum, and range.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-11 3:09am    
How did you understand the question? Anyway, my 5.
--SA
OriginalGriff 7-Mar-11 3:26am    
Practice and a brain like a pretzel! :laugh:
Sergey Alexandrovich Kryukov 7-Mar-11 14:50pm    
Hope you don't feel much pain :-)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900