I'm using RegularExpressionValidator controls to impose password rules when creating new passwords. I want to avoid repeatitons such as 'aaaa' or sequences such as '12345' in the password.
How do I write the validationexpression to a RegularExpressionValidator to impose this rule? I do not want to do thing using C#, just with a regular expression that could be put within the validator control like below:
matches any word character (letter, digit, or underscore) and the
\\1+
matches whatever was in the first set of parentheses, one or more times. So you wind up matching any occurrence of a word character, followed immediately by one or more of the same word character again.
Actually, what I want to do is to AVOID repeatition - i.e. if a character occurs more than twice adjecent, the validation should fail. Sorry for not being clear in the previous post.
According to your description ,I think you can use JavaScript to authenticate the value of password.Here is a sample ,it shows how to check the value whether it is repeat or sequence. Hope it can help you.
1Code in .aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
var error = "";
var begin
var first
var second
function keyPress(id) {
var tb = document.getElementById(id);
if (tb.value.length > 2) {
var message = tb.value.substring(tb.value.length - 3, tb.value.length);
if (checkRate(message)) {
begin = message.substring(0, 1);
first = message.substring(1, 2);
second = message.substring(2, 3);
if (begin.toString() == first.toString() && first.toString() == second.toString()) {
error = "your message is repeat";
alert(error);
}
else {
error = "";
}
}
else {
begin = message.substring(0, 1);
first = message.substring(1, 2);
second = message.substring(2, 3);
if (parseInt(first) == parseInt(second) - 1 && parseInt(begin) == parseInt(first) - 1) {
error = "your message is sequences";
alert(error);
}
else {
error = "";
}
}
}
return false;
}
function checkRate(input) {
var re = /^[1-9]+[0-9]*]*$/;
if (!re.test(input)) {
return true;
}
else {
return false;
}
}
function Iserror() {
if (error == "") {
return true;
}
else {
alert(error);
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick="return Iserror();" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
Thanks, I finally had to go with the Javascript method although I intended to do it with a RegualrExpression validator. For some reason, it didn't work out. Your JS code was really useful, thanks again :)
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegTxtName" runat="server"
ErrorMessage="Repeated Character Not Allowed Password Must Contain A Number" ControlToValidate="TxtName"
ValidationExpression="^(?!.*(.)\1)\w{6,10}$" ></asp:RegularExpressionValidator>
chathu03j
Member
72 Points
113 Posts
Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 10:41 AM|LINK
Hi,
I'm using RegularExpressionValidator controls to impose password rules when creating new passwords. I want to avoid repeatitons such as 'aaaa' or sequences such as '12345' in the password.
How do I write the validationexpression to a RegularExpressionValidator to impose this rule? I do not want to do thing using C#, just with a regular expression that could be put within the validator control like below:
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Password should contain Minimum 8 Characters" ControlToValidate="txtPwd" ValidationExpression="\w{8,30}"><asp:RegularExpressionValidator>
Thanks in advance
Chathu
Associate Software Engineer - Colombo
VampireCoder
Member
132 Points
31 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 10:59 AM|LINK
Try
The
matches any word character (letter, digit, or underscore) and the matches whatever was in the first set of parentheses, one or more times. So you wind up matching any occurrence of a word character, followed immediately by one or more of the same word character again.chathu03j
Member
72 Points
113 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 11:13 AM|LINK
H,
Actually, what I want to do is to AVOID repeatition - i.e. if a character occurs more than twice adjecent, the validation should fail. Sorry for not being clear in the previous post.
Chathu
Associate Software Engineer - Colombo
VampireCoder
Member
132 Points
31 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 11:21 AM|LINK
Have a look this..
http://www.camilleri.me/?p=424
VampireCoder
Member
132 Points
31 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 11:26 AM|LINK
And If You want the Character comes only one Occurance in the String then use this Regular Expression.
(?i:([A-D])(?!\1)([A-D])(?!\1|\2)([A-D])(?!\1|\2|\3)([A-D]))
Description: This regex validates a string of non-repeating characters A, B, C, D.
Matches : abcd | dbca | badc
Non-Matches: abba | baaa | cabb
Note: If You want A,B,C,.....X,Y,Z then Just Change the [A-D] to [A-Z].
chathu03j
Member
72 Points
113 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 09, 2010 12:31 PM|LINK
Hi,
It did not work for me :( following is the aspx code for the validator control:
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ValidationExpression="^(?i:([A-Z])(?!\1)([A-Z])(?!\1|\2)([A-Z])(?!\1|\2|\3)([A-Z]))$" ErrorMessage="Error" ControlToValidate="txtPwd" Width="100%"></asp:RegularExpressionValidator>
I even used ValidationExpression ="^(?i:([A-Z])(?!\1)([A-Z])(?!\1|\2)([A-Z])(?!\1|\2|\3)([A-Z]))$"
but still it does not validate. i.e. I can type a password like jghu78aaaa and it still validates as correct.
Chathu
Associate Software Engineer - Colombo
Ming Xu - MS...
All-Star
25269 Points
2235 Posts
Microsoft
Re: Regualr Expression to validate repeated subset characters in a password
Aug 12, 2010 05:37 AM|LINK
Hi,
According to your description ,I think you can use JavaScript to authenticate the value of password.Here is a sample ,it shows how to check the value whether it is repeat or sequence. Hope it can help you.
1Code in .aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script language="javascript" type="text/javascript"> var error = ""; var begin var first var second function keyPress(id) { var tb = document.getElementById(id); if (tb.value.length > 2) { var message = tb.value.substring(tb.value.length - 3, tb.value.length); if (checkRate(message)) { begin = message.substring(0, 1); first = message.substring(1, 2); second = message.substring(2, 3); if (begin.toString() == first.toString() && first.toString() == second.toString()) { error = "your message is repeat"; alert(error); } else { error = ""; } } else { begin = message.substring(0, 1); first = message.substring(1, 2); second = message.substring(2, 3); if (parseInt(first) == parseInt(second) - 1 && parseInt(begin) == parseInt(first) - 1) { error = "your message is sequences"; alert(error); } else { error = ""; } } } return false; } function checkRate(input) { var re = /^[1-9]+[0-9]*]*$/; if (!re.test(input)) { return true; } else { return false; } } function Iserror() { if (error == "") { return true; } else { alert(error); return false; } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Iserror();" onclick="Button1_Click" /> </div> </form> </body> </html>2.Code in .cs:
protected void Page_Load(object sender, EventArgs e) { this.TextBox1.Attributes.Add("onkeypress", "keyPress('TextBox1')"); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("It is all right"); }Feedback to us
Develop and promote your apps in Windows Store
chathu03j
Member
72 Points
113 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 15, 2010 02:34 PM|LINK
Hi Ming Xu ,
Thanks, I finally had to go with the Javascript method although I intended to do it with a RegualrExpression validator. For some reason, it didn't work out. Your JS code was really useful, thanks again :)
Chathu
Associate Software Engineer - Colombo
SohailShaikh
Contributor
6281 Points
1188 Posts
Re: Regualr Expression to validate repeated subset characters in a password
Aug 05, 2012 01:20 PM|LINK
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegTxtName" runat="server"
ErrorMessage="Repeated Character Not Allowed Password Must Contain A Number" ControlToValidate="TxtName"
ValidationExpression="^(?!.*(.)\1)\w{6,10}$" ></asp:RegularExpressionValidator>
use this its work for me
Shaikh sohail
Sohail Shaikh