المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : simple PAssword Policy



C# Programming
12-17-2009, 06:21 PM
Hi All

I am struggling to make a regular expresssion for the password policy.
There are only two requiremnts for the password policy

1. Password should of length atleast 6
2. password must contain atleast two characters at any position.

Match cases(for which regex should pass) are
1. rt5465465
2. 6556h76f
3. 76d12j
4. s45)$f

Cases for which regex should fail are
1. w565765
2. 872310
3. 4r@%&9
4. gghj

The regex whixh I have made so far are in the below code

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

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

//Regex regex = new Regex("(.*[a-zA-Z].*[a-zA-Z])");
// Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(?(.*[a-zA-Z].*[a-zA-Z]))(.{6,})");

Regex regex = new Regex("(^[.]*${6,})");

//Regex regex = new Regex("([a-zA-Z0-9]{6,})");
//Regex regex = new Regex("(?(?=[.]{6,})(.*[a-zA-Z].*[a-zA-Z])([Z][9][4][M][P]))");

// Regex regex = new Regex("(?(^(?=.+[a-zA-Z$#%^&*()!@~+]){6,}))(.*[a-zA-Z].*[a-zA-Z])");
// Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])([6])([1]{5,5}))");
// Regex regex = new Regex("(?(?=[0-9]{5,5}[a-z])()([1]{5,5}))");
//Regex regex = new Regex("(?(?=[a-zA-Z0-9]{6,})()(([1])([1])([p])))");
string s = textBox1.Text;

if (regex.IsMatch(s))
{

MessageBox.Show("pass");

}

else
{

MessageBox.Show("fail");

}


}
}
}

Please help

Thanks

Regards
Sandeep