I want the user to make a valid selection in a DropDownList, but I don't want to present any selection as the default so the default selected value is -- Select --. I am using a RegularExpressionValidator to only accept selected values that contain a literal comma (since the valid items are formatted LastName, FirstName).
However, I am perplexed as to how to get the validator to look for the Selected value! I'd really rather NOT make this a server-level validation, and if possible I'd like to keep it as simple as a Regex Validator.
Here's the code:
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegexValidator" runat="server"
ControlToValidate="ddNames" ValidationExpression=".*\,.*"
ErrorMessage="Select a Valid User">
</asp:RegularExpressionValidator>
The dropdown values are:
-- Select --
Smith, John B
Jones, Bill
Wilkinson, Harmony
Hull, Cordell
Hill, Faith
Is there a way to aim the validator at the SelectedValue? As shown, the Validator seems not to be seeing any of the values in the control.
Am I right that you want to force the user to make a selection, and that selection should fit into your regex?
If so, you can easily add the RequiredFiledValidator class to your dropdown control, provide the InitialValue property to it equal to your -- Select -- item value so user will be forced to select something not equal to the -- Select --, and this value will be validated via RegularExpressionValidator control.
From MSDN:
Validation fails only if the value of the associated input control matches this InitialValue upon losing focus.
The strings in both the InitialValue property and the input control are trimmed to remove extra spaces before and after the string before validation is performed.
Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.
Also you should note for a RegularExpressionValidator class, that
Both server-side and client-side validation are performed unless the browser does not support client-side validation or client-side validation is explicitly disabled (by setting the EnableClientScript property to false).
So your code should be something like this:
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RegularExpressionValidator ID="RegexValidator" runat="server"
ControlToValidate="ddNames" ValidationExpression=".*\,.*"
ErrorMessage="Select a Valid User" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="ddNames"
InitialValue="-- Select --" />
you can use RequiredFieldValidator.
<asp:DropDownList ID="ddNames" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please select" ControlToValidate="ddNames"
InitialValue="-- Select --"></asp:RequiredFieldValidator>
Related
I am using a dropdown within an EditTemplate as such:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server" DataSourceID="BusinessTypeSource" DataTextField="Value" DataValueField="Value" AppendDataBoundItems="true" Text='<%# Bind("BusinessType") %>'>
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
The DataSource has the following value:
Personal
Professional
The problem that I am running into is that the field that I am binding has a blank value.
As a blank value is not in the DataSource I get the following error message:
'ddlBusinessType' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value
Not sure how to fix this. When I do my binding, if the value is not in the datasource, I like to default it to 'Please Select'
Depends on the circumstances, but one option I'll use often times is as follows:
1) In the lookup table, I'll have an entry titled 'Not Selected',
with a primary key value of 0 (or whatever).
2) In the transaction table, update all of the null values for that field to 0 (or whatever the value is for your 'Not Selected' option). Also, set the default value of that field to 0.
This is nice for a couple of reasons -- one, no special mark-up required (so you know it will always be consistent) -- and two, wherever else you use this data, you'll have access to the 'not selected' output.
It should be like:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server"
DataSourceID="BusinessTypeSource"
DataTextField="BusinessType" DataValueField="Value" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
1) No need to set Text as you already bounded the text field.
2) You need to set a value for default item you have added in markup.
You are trying to select an item before binding process. Capture Binding event and set de selected value on this place.
Would like to validate more than one control on one button click. I would like something to validate whether a textbox has contents if a checkbox is checked or not but the checkbox doesn't necessarily have to be checked and in that case I don't want to check the textbox. I tried validation group but each button needs to control the different groups and i need this all to be under one button.
I'm open to ideas of how to do this c#,javascript...etc. Heres some code: Button3 is the save which validates whether checkbox 1 is checked and if so textbox10 cant be empty. I have about four other instances of this but are independent of each other.
<asp:Button ID="Button3" runat="server" Height="24px"
Text="Save" Visible="False" Width="67px" Font-Bold="True"
causesvalidation="true"
validationgroup="required"
runat="Server" />
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" Text=" Breach Letter Sent"
ValidationGroup="required" AutoPostBack="True" Enabled="False" />
You want to use the CustomValidator control which can validate both on the server and the client. There is an example in the docs here - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
I would never do form validation in JavaScript. Believe it or not, but some people actually turn off JavaScript! Use validators to validate the field content. Of course this means a round trip to the server in most cases, but you get reliable and well integrated validation.
you can use validation with Ajax (in Ajax postback occures but you will not sense)
I succesfully used a validator more than once,but after some programming my validators are not working.Maybe is something i don't know about defining 2 validators for the same control, but it doesn't work for one validator in a control either.Here are 2 examples of my code:
Example 1: one required field validator and one "maximum value" validator for username:
<asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Dynamic" ErrorMessage="Required" />
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="UsernameTextbox" MinimumValue="1" MaximumValue="20" ForeColor="red"
Display="Dynamic" ErrorMessage="Name must contain maximum 20 characters"></asp:RangeValidator>
Example 2: one "maximum value" validator for roadaddress(string):
<asp:RangeValidator ID="RangeValidator9" runat="server" MaximumValue="50" ForeColor="red"
ErrorMessage="Road Address must contain maxmum 50 characters" ControlToValidate="RoadAddressTextbox"></asp:RangeValidator>
I am thinking that the problem is maybe in the display property or in the causesvalidation property which i don't use...
That's not what the RangeValidator is used for. The RangeValidator is intended to check input to make sure it's within a certain range, i.e. to make sure that a number is between 1 and 5, that a date is within a certain range, etc.
What you need is a RegularExpressionValidator:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="UserNameTextbox"
ErrorMessage="Username must be between 1 and 50 characters"
ValidationExpression="^[a-zA-Z\s]{1,50}">
</asp:RegularExpressionValidator>
EDIT: Updated expression to ^[a-zA-Z\s]{1,50}
RangeValidators don't validate the number of characters in the input, they "Check whether the value of an input control is within a specified range of values."
You can actually do this without a Validator, by setting the MaxLength property on the text box which would limit the number of characters entered into it.
<asp:TextBox ID="UserNameTextbox" MaxLength="50" runat="server"></asp:TextBox>
Basically, I have 2 textbox's which the user enters currency/numeric values - I want to have a validator that checks if the values in these box's add up to make another value. Is there a built in validator for this or would I have to use a custom validator?
You need a custom validator for that. There exist no standard validators in ASP.NET that do the sum checking you require.
See also:
http://asp.net-tutorials.com/validation/custom-validator
http://msdn.microsoft.com/en-us/library/f5db6z8k(v=vs.71).aspx
http://www.codeproject.com/KB/validation/aspnetvalidation.aspx
Good luck!
if bothe values are required :
so 2 of
<asp:RequiredFieldValidator ID="VAL_Req_Pass" runat="server" ControlToValidate="TXT_Password"
Display="Dynamic" ValidationGroup="User">Required</asp:RequiredFieldValidator>
and one of :
<asp:CustomValidator ID="CustomValidator2" runat="server" ValidationGroup="User"
ErrorMessage="Password does not match"
ClientValidationFunction="passwordMatch"
ControlToValidate="TXT_Password"
Display="Dynamic" >
</asp:CustomValidator>
which has js function that checks if equal...
I have page user creation and it contains textbox control. I want to restrict user entering html tag in i.e < and > sign in textbox using .net validation control.
Can any one help me about in this?
I also want to restrict double quote i.e " and caret sign ^ can you please tell me how to write expression for that???
Use a regularexpressionvalidator...
<asp:textbox id="theTextbox" runat="server" />
<asp:regularexpressionvalidator id="regexValiator" runat="server"
controltovalidate="theTextbox"
errormessage='<, >, ", and ^ not allowed'
display="Dynamic"
validationexpression='([^<>\"\^])*' />
Actually, by default ASP.Net dissallow HTML content to be entered in form fields. No need for further validation.
you can try the following code in your aspx code:
<asp:textbox id="txtBox" runat="server" />
<asp:RegularExpressionValidator controltovalidate="txtBox" ValidationExpression="([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])" ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>
and now you can modify the regular expression to fit your case.