Visual Studio .NET - Regular expression to validate string

Asked By James John on 14-May-08 09:09 AM
This is my drop down list:

<asp:DropDownList ID="DropDownList1" runat="server" >
         <asp:ListItem Selected="True" Text="Select"></asp:ListItem>
        <asp:ListItem Selected="True" Text="ASP"></asp:ListItem>
       <asp:ListItem Selected="True" Text="JAVA"></asp:ListItem>
       <asp:ListItem Selected="True" Text="C#"></asp:ListItem>
</asp:DropDownList>

Selected item will be "Select".
Suppose, if a user forgot to select an item other than "Select", I want to display an error message "Please select an item" using RegularExpressionValidator

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please select an item" SetFocusOnError="True"  ValidationExpression=" ">
</asp:RegularExpressionValidator>

What will be the ValidationExpression?

You need to write custom validator for this - Shailendrasinh Parmar replied to James John on 14-May-08 09:12 AM

End of post

You have multiple selected items in your definition. - Peter Bromberg replied to James John on 14-May-08 09:16 AM

DropdownList only supports one ListItem with the "selected=True" attribute.

here it is - Santhosh N replied to James John on 14-May-08 09:48 AM

You can valiadate based on the selectedIndex value..

if (DropDownList1.SelectedIndex == 0)
    ---your validation code here..

And one more thing to consider is remove selected for all the other items except for Select as Peter mentioned..
cheers
Check this - sri sri replied to James John on 14-May-08 10:43 PM
Hi,
You can validate the DropDownlist by using the Required field Validator.
Regular expression validator are used to validate the values in the textbox for emailid, date, time and others.

check the below code. This will work fine for your requirements

<asp:DropDownList ID="DropDownList1" runat="server" >
         <asp:ListItem Selected="True" Text="Select"></asp:ListItem>
        <asp:ListItem Selected="True" Text="ASP"></asp:ListItem>
       <asp:ListItem Selected="True" Text="JAVA"></asp:ListItem>
       <asp:ListItem Selected="True" Text="C#"></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="Please Select an Item" InitialValue="Select"></asp:RequiredFieldValidator>
use Requiredfiledvalidator... - Vasanthakumar D replied to James John on 15-May-08 01:32 AM

Hi,

use Required filed validator and set its Initial value to "Select" .. check this...

<asp:DropDownList ID="DropDownList1" runat="server" >
         <asp:ListItem Selected="True" Text="Select"></asp:ListItem>
        <asp:ListItem Text="ASP"></asp:ListItem>
       <asp:ListItem Text="JAVA"></asp:ListItem>
       <asp:ListItem  Text="C#"></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="DropDownList1" InitialValue="Select" ErrorMessage="Please select an item"></asp:RequiredFieldValidator>

simple solution - Nagaraj muthuchamy replied to Peter Bromberg on 15-Jul-08 05:22 AM

Experiment the Regular Expressions with Stored procedure.

http://dotnet-globalization.blogspot.com