I'm working on an ASP.Net application and I need to make sure my password field contains at least one special character.
This is what I have:
<asp:TextBox ID="Password" runat="server" TextMode="Password" />
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ForeColor="Red"
ErrorMessage="Password Required" ValidationGroup="vgErrors"
ControlToValidate="Password" Display="Dynamic" Text="*" />
Then I need to add an <asp:RegularExpressionValidator >
to this field as well which needs to take the following special characters: (including a space)
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
I'm struggling to implement this because I'm not too familiar with the <asp:RegularExpressionValidator >
control, and I'm having some issues since they're special characters.
You can use the following regular expression to match a password containing between 8 and 16 characters, including one special character from your list. You can change at your need the range of total characters allowed:
^(?=.*\w)(?=.*[ !"#$%&'()*+,-./:;<=>?@[\]\^_`\{\|\}\~]).{8,16}$
In your aspx page, add a RegularExpressionValidator
:
<asp:RegularExpressionValidator ID="revPassword" ControlToValidate="Password"
ForeColor="Red" ValidationGroup="vgErrors" runat="server" ErrorMessage="Error Message"/>
In the code-behind of your aspx page, you can set the ValidationExpression
of your RegularExpressionValidator
:
revPassword.ValidationExpression =
"^(?=.*\\w)(?=.*[ !\"#$%&'()*+,-./:;<=>?@[\\]\\^_`\\{\\|\\}\\~]).{8,16}$";