Pori
Pori

Reputation: 696

Why is ASP.Net validation only working partially?

I am trying to write a form with validation using Visual Basic.Net with ASP.

I dropped a RegularExpressionValidator into a form field for testing and assigned the attributes through the Visual Basic code in the Page_Load function, like this:

LoginID.ValidationExpression = "[\\s\\S]{0,3}"
LoginID.ErrorMessage = "Maximum 3 characters are allowed."

I set up a three character max for testing purposes. The input for that text field will be invalidated no matter the length and the error message is not updated from it's stock value.

Why is this happening and how can I fix it?

EDIT:

The regular expression I have works just fine. I've tested it already. Something else must be the problem.

Here is the bulk of the code for those who need to see more. Outside of this, I don't see anything else being relevant:

<%-- ASP code %>
<th width="200"><span class="required">*</span>Create Login ID :</th>
     <td width="230" align="left"><asp:TextBox ID="txtLoginID" runat="server"          CssClass="inputbox" MaxLength="50"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="First" CssClass="ValueValidator" ControlToValidate="txtLoginID" Display="Dynamic" runat="server" ErrorMessage="<br>Please Enter LoginID"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="ValRegExLoginID" runat="server" ControlToValidate="txtLoginID" CssClass="ValueValidator" ErrorMessage="*"></asp:RegularExpressionValidator>

</td>

//Visual Basic Code
Protected Sub Page_Load(ByVal source As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
    ValRegExLoginID.ValidationExpression = "[\\s\\S]{0, 3}"
    ValRegExLoginID.ErrorMessage = "Maximum 3 characters are allowed."
End If

End Sub

Upvotes: 0

Views: 108

Answers (1)

Sev
Sev

Reputation: 781

Regex for Maximum 3 characters are allowed is:

^.{0,3}$

So replace

LoginID.ValidationExpression = "[\\s\\S]{0,3}"

with

LoginID.ValidationExpression = "^.{0,3}$"

Upvotes: 0

Related Questions