ASP.NET Web Control Validation Woes...

liunx

Guest
I've been working on this form all day, and I'm kind of stumped. Is there a way to use the built-in validator web controls from ASP.NET to limit characters as opposed to values?

For instance, I know that:
<asp:RangeValidator id="valRGtextbox1" runat="server" ControlToValidate="textbox1" MinimumValue="1" MaximumValue="100" Type="String" Text="REQUIRED"/>
...results in the user being able to enter any value from 1 to 100.

And I know that using the "RegularExpression" validator like this:
<asp:RegularExpressionValidator id="valREtextbox1" ControlToValidate="textbox1" ValidationExpression="^\d{10}$" Display="Static" runat="server" Text="REQUIRED: 1-100 chars"/>
...results in the user having to enter 100 characters for it to be valid.

But is there a way to use one of the built-in validators to do the same thing with characters without creating your own script? Any suggestions would be greatly appreciated.If this is a simple <input> (a text box that is not multiline) you can stop the user from entering too many characters very easily with the html maxlength attribute. I do not know how you would check it with a built in validator if this is a multiline textbox. You can easily check it with len though.I can limit the number of characters length-wise, but I want an actual validation message to come up next to the textbox in the ASP.NET GataGrid. Any suggestions would be greatly appreciated. Thanks.Originally posted by kwilliams
I can limit the number of characters length-wise, but I want an actual validation message to come up next to the textbox in the ASP.NET GataGrid. Any suggestions would be greatly appreciated. Thanks. is this an <asp:textbox> Or is this an html one? If it is html it must be hand coded. Like I said before you can check the length using len, run an if statement based on that and write out the message or fire the code.It's the <asp:textbox> one. I actually came up with a good workaround that uses both the RequiredFieldValidator and the RegularExpressionValidator. Here's the code for that part now:

<asp:Textbox runat="server" Columns="20" id="txtcontactID_edit" Width="20px" Text='<%# DataBinder.Eval(Container.DataItem,"contactID") %>' />
<asp:RequiredFieldValidator id="varvalRFcontactID" runat="server" Text="REQUIRED: 1-3 chars" ControlToValidate="txtcontactID_edit" />
<asp:RegularExpressionValidator id="valREcontactID" runat="server" ControlToValidate="txtcontactID_edit" Text="REQUIRED: 1-3 chars" ValidationExpression="\d{1,3}" />

The "RequiredFieldValidator" makes the user enter some value, and the "RegularExpressionValidator" limits the characters from 1 to 3. I was able to do this without any server-side script, and it works great. So I guess this problem is solved; thanks for all of your help though.
 
Back
Top