2017-07-09 8 views
2

現在、特定の条件に一致したときのみパスワードを許可する必要があるWindowsフォームを作成しています。私はちょうど1つの要素がついています。 "パスワードの最初と最後の文字は数字でなければなりません"。テキストボックスの最初と最後の文字が数字であることを確認します。c#

私の現在のコードを見てください私はゼロアイデアを持っているので、これについてどうやって行くのですか?赤ちゃんの歩みと忍耐は評価されるでしょう。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Text.RegularExpressions; 

namespace password_test 
{ 
    public partial class Form1 : Form 
    { 

     bool Finished = false; 


     private bool TestPassword(string passwordText, int minimumLength =  5, int maximumLength = 12, int minimumNumbers = 1, int minimumLetters = 1) 
    { 


     int letters = 0; 
     int digits = 0; 
     int minLetters = 0; 


     if (passwordText.Length < minimumLength) 
     { 
      MessageBox.Show("You must have at least " + minimumLength + " characters in your password."); 
      return false; 
     } 

     if (passwordText.Length > maximumLength) 
     { 
      MessageBox.Show("You must have no more than " + maximumLength + " characters in your password."); 
      return false; 
     } 

     foreach (var ch in passwordText) 
     { 
      if (char.IsLetter(ch)) letters++; 
      if (char.IsDigit(ch)) digits++; 

      if (ch > 96 && ch < 123) 
      { 
       minLetters++; 
      } 
     } 


     if (digits < minimumNumbers) 
     { 
      MessageBox.Show("You must have at least " + minimumNumbers + " numbers in your password."); 
      return false; 
     } 

     if (minLetters < minimumLetters) 
     { 
      MessageBox.Show("You must have at least " + minimumLetters + " letter in your password"); 
      return false; 
     } 
     Finished = true; 
     return true; 


    } 


    public Form1() 
    { 
     InitializeComponent(); 
    } 


    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void butConfirm_Click(object sender, EventArgs e) 
    { 





     if (txtPassword.Text == txtRetypePassword.Text) 
     { 
       bool temp = TestPassword(txtPassword.Text, 10, 100, 1, 1); 


      if (Finished == true) 
      { 
       MessageBox.Show("Password Accepted"); 
       Application.Exit(); 
       //this.Hide(); 
       //Form2 f2 = new Form2(); 
       //f2.ShowDialog(); 

      } 
     } 


     else 
     { 
      MessageBox.Show("Please ensure the passwords you have typed match"); 
      return; 
     } 


    } 

    private void txtPassword_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (!char.IsControl(e.KeyChar) && !char.IsLetterOrDigit(e.KeyChar)) 
     { 
      e.Handled = true; 
     } 
     else 
     { 
      return; 
     } 
    } 

    private void txtRetypePassword_TextChanged(object sender, EventArgs e) 
    { 

    } 




} 
} 

答えて

0

Finished = true;する前に、あなたは単にあなたのチェックを行うことができます。

if (!char.IsDigit(passwordText[0]) || !char.IsDigit(passwordText[passwordText.Length - 1])) 
{ 
    MessageBox.Show("The first and last characters of the password have to be numbers"); 
    return false; 
} 
Finished = true; 

また、あなたの方法で最小長を強制する必要があります。

+0

ありがとうございました! –

+0

問題ありません。答えを正しいものとしてマークして、質問を見ている他の人が回答が助けになったことを知ることができます。 –

+0

どうすれば最初と最後の文字が数字ではないので、これを逆にしますか? –

0

それとも、逆に正規表現Test Pattern

// ^\d = start with digit 
    // \d$ end with digit 
    // .{3,10} including start and ending, min 5 max 12 
    if (!Regex.IsMatch(str, @"\d.{3,10}\d$") 
    { 
     // Invalid 
    } 

使用\ Dを使用することができます。

関連する問題