Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validator for ListBox

Status
Not open for further replies.

groston

IS-IT--Management
Dec 31, 2001
141
US
Is it possible to use one of the provided validators to determine if a ListBox has at least one item selected? If so, how does one do this?

Thanks!



----

Gerry Roston
gerry@pairofdocs.net
 
It's possible. In this sample, the validator's error message will appear when any of page controls posts back, or during selection on the list box - if all items are unselected.
Code:
<asp:ListBox ID=&quot;lb&quot; Runat=server SelectionMode=Multiple>
 <asp:ListItem Value=&quot;1&quot;>Item 1</asp:ListItem>
 <asp:ListItem Value=&quot;2&quot;>Item 2</asp:ListItem>
 <asp:ListItem Value=&quot;3&quot;>Item 3</asp:ListItem>
</asp:ListBox>
<asp:RequiredFieldValidator ID=&quot;rvlb&quot; Runat=server ControlToValidate=&quot;lb&quot; InitialValue=&quot;&quot; ErrorMessage=&quot;Select Item&quot; />
 
LV,

Thanks for the quick reply - I'll give it a try!



----

Gerry Roston
gerry@pairofdocs.net
 
Works just fine - Thanks!

p.s. While I've got your attention: is it possible to use a datavalidator to ensure that a specific number of characters are entered into a textbox?



----

Gerry Roston
gerry@pairofdocs.net
 
You can set the MaxLength property of a TextBox to limit the number of characters entered. If you need to ensure that a specific number of characters is entered, then the best solution would be to use a RegularExpressionValidator or CustomValidator. I'm not an expert in RegEx, but have used CustomValidators.
Code:
<head>
 <script language=javascript>
  function checkChars(source, arguments){
    arguments.IsValid = true;
    var len = arguments.Value.length;
    // ensure that 5 characters are entered
    if(len != 5){
      arguments.IsValid = false;
    }
  }
 </script>
</head>
<asp:TextBox ID=&quot;myText&quot; Runat=server />
<asp:CustomValidator ID=&quot;vldMyText&quot; Runat=server ErrorMessage=&quot;Invalid number of characters&quot; ControlToValidate=&quot;myText&quot; ClientValidationFunction=&quot;checkChars&quot; />
 
LV,

Again, thanks for your help. This worked out well, with one exception - when the user hits a 'cancel' button on the page, the validator still fires. I guess I need to figure out how to determine the event that causes a page reload and skip the validation code if the 'cancel' button was hit.



----

Gerry Roston
gerry@pairofdocs.net
 
For your &quot;Cancel&quot; button: set the CausesValidation property to False:
Code:
<ASP:Button id=&quot;btnCancel&quot; runat=server Text=&quot;Cancel&quot; CausesValidation=False />
 
LV,

I must be getting old! I had figured this out once before, but since that was more than two days ago, the lesson was promptly forgotten!

Thanks again!

----

Gerry Roston
gerry@pairofdocs.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top