It's possible to block past dates in ASP.net TextBox TextMode = "DateTime" or "Date"
I assume this is WebForms, in which case yes, you can use a RangeValidator. Like so:
<asp:TextBox runat="server" ID="dateField" />
<asp:RangeValidator runat="server" Type="Date" ControlToValidate="dateField"
MaximumValue="9999/12/28"
MinimumValue="1000/12/28"
ErrorMessage="enter valid date" />
You'll need to set the MinimumValue attribute as appropriate. You can do this from your codebehind, or an in-page <script> block:
this.rangeValidator.MinimumValue = DateTime.UtcNow.Subtract( new TimeSpan(13,0,0) ).ToString("G", CultureInfo.InvariantCulture);
Note my use of UtcNow and subtracting 13 hours to get a true, globally-acceptable, minimum date value. You might want to change this as appropriate for your application (such as restricting it to the timezone of your server provided that's what your users expect).
This post should help you
Input element specification The min attribute, if specified, must
have a value that is a valid global date and time string. The max
attribute, if specified, must have a value that is a valid global date
and time string
You will probably do this from code behind
yourTextBox.Attributes.Add("min", DateTime.Now.ToString());
Related
So I have a scenario where I need to change the filter type of an Filteredtextboxextender based on a value in the config file in my asp.net project.
This change propagates through about 80 input forms currently.
Since my web.config value is a string I need to do an if statement on every page to define the filter type.
e.g.
If ConfigurationManager.AppSettings("SomeVariable") = "A" Then
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.LowercaseLetters Or AjaxControlToolkit.FilterTypes.UppercaseLetters Or AjaxControlToolkit.FilterTypes.Numbers
ElseIf ConfigurationManager.AppSettings("SomeVariable") = "B" Then
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.Numbers
Else
txtLTWo_Filteredtextboxextender.FilterType = AjaxControlToolkit.FilterTypes.Custom
txtLTWo_Filteredtextboxextender.ValidChars = "!QW"
End If
The problem here is that a change to a filter type for a particular result would have me changing every page.
I would really like a way to define it in the config file directly.
Something like this, but obviously doesn't work.
txtML_Wo_Filteredtextboxextender.FilterType = ConfigurationManager.AppSettings("FilterType")
The only way to do it that I can come up with is to keep the filter as AjaxControlToolkit.FilterTypes.Custom
And then define the string of valid characters in the config file. However this would be a large string and seems senseless when the filter has the attributes I need.
Any ideas to streamline this would be great, thanks in advance.
Hum some ideas to float here:
On page pre render (not 100% sure of the event), the you can/could inject that code.
Or you simple have a global function in a code module that fetches the config settings you have, and then use this:
this:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender"
runat="server" BehaviorID="TextBox1_FilteredTextBoxExtender"
TargetControlID="TextBox1"
FilterType="LowercaseLetters" >
</ajaxToolkit:FilteredTextBoxExtender>
becomes:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="TextBox1_FilteredTextBoxExtender"
runat="server" BehaviorID="TextBox1_FilteredTextBoxExtender"
TargetControlID="TextBox1"
FilterType="<%= MyFilterType() %>" >
</ajaxToolkit:FilteredTextBoxExtender>
Or say Module1.FilterType().
So, we could say have a global vb function, and then use a sever side expression in the markup settings.
I don't know what (if) those values like Filter Type "LowercaseLetters" are string or enum???
However, a server side expression in the markup could and should work.
Public Function MyFilterType() as string ???
code here to fetch the setting from config
return strSetting
End Function
Is it possible in an ASP page to define a variable, then use that variable for a web control's attribute value?
I'm trying to add validation controls to my ASP.NET page, and one of them is a regex control. The expression I want will check that a field only contains numbers, spaces, plus sign and round brackets. (for phone numbers) Now round brackets are a special character that can't be escaped with a slash (as far as I can tell), so what I did was use Regex.Escape() to construct my regular expression, which I stored as a string variable. I now want to use that variable as the value for the regex validation control's expression attribute. Is this possible? If not, how can I achieve the validation I want?
EDIT1: So, in the tag of my ASP page I have this:
<% string PhoneNumber = "[^0-9 |^" + Regex.Escape("(") + " |^" + Regex.Escape(" "); %>
Then further down I have this:
<asp:RegularExpressionValidator ID="MobilePhoneValidator" runat="server" ControlToValidate="MobilePhone" ErrorMessage="Mobile phone number must only contain numbers." ForeColor="Red" ValidationExpression=PhoneNumber></asp:RegularExpressionValidator>
I'm pretty sure that using ValidationExpression="PhoneNumber" will use the actual string PhoneNumber and not look up my variable. Though to be honest, I haven't actually tried that.
I don't know why you spcified a string in markup rather than code behind, but anyway.
Assigning this string to your validator looks like this then:
<asp:RegularExpressionValidator ID="MobilePhoneValidator" runat="server" ControlToValidate="MobilePhone" ErrorMessage="Mobile phone number must only contain numbers." ForeColor="Red" ></asp:RegularExpressionValidator>
<% string PhoneNumber = "[^0-9 |^" + Regex.Escape("(") + " |^" + Regex.Escape(" ");
MobilePhoneValidator.ValidationExpression = PhoneNumber; %>
Or you could just define it in your code file, rather then markup.
i have an asp,net textbox like this
<asp:TextBox readonly="true" ID="txtLastService" runat="server"></asp:TextBox>
when i tried to get its value to a date variable LastService in code behind, i get this error
Conversion from string "" to type 'Date' is not valid.
Exception
Details: System.InvalidCastException: Conversion from string "" to
type 'Date' is not valid.
Source Error: Line 26: oItem.LastService = txtLastService.Text Source File: .\admin\vehicle\add.aspx.vb Line:
26
i have used this same code in other pages, and it works fine, except for this page
any help
EDIt
Please note that the value of the textbox is not empty when i click submit on the form. And also when i remove the readonly attributes, i don;t get the error. But i need this textbox to be readonly since am using javascript to select date and setting its value
Instead of Readonly="true", try using Enabled="false"
The error seems extremely self-explanatory to me. The string is "" and you are trying to convert it to a Date. So how the heck can an empty string be converted to a Date? What date would that be?
Why not use a HiddenField instead?
<asp:hiddenfield id="txtLastService" runat="server"/>
Try using Request.Form[txtLastService.UniqueID] if C# and
Request.Form(txtLastService.UniqueID) if vb.net
Using a asp validator, is there a way to verify the date of birth that is entered is within an age range, say 5 and 22 years old?
I currently do a asp:compareValidator and a asp:rangeValidator, but i need to add another level of range validation for current date against the value entered in the textbox to notify that the age is outside the range allowed (5 and 22 years old only)
thanks
tony
You could use an ASP.NET CustomValidator for this. In your ASP.NET code, you add the following control:
<asp:CustomValidator runat="server" id="ageByDateCheck"
ControlToValidate="txtDate"
OnServerValidate="CheckAgeByDate"
ErrorMessage="You are not between the ages of 5 and 22." />
And in your code-behind, you'd have a new method for checking the age.
public void CheckAgeByDate(object source, ServerValidateEventArgs args)
{
var date = DateTime.Parse(args.Value);
args.IsValid = true; //Replace this with your age check algorithm.
}
EDIT
If you're looking for client-side validation, you'll have to create some javascript to do your validation and specify it in the control's ClientValidation field. So your ASP.NET control now looks something like this:
<asp:CustomValidator runat="server" id="ageByDateCheck"
ControlToValidate="txtDate"
OnServerValidate="CheckAgeByDate"
ClientValidation="ClientValidate"
ErrorMessage="You are not between the ages of 5 and 22." />
Your probably don't have to have a different name for that, but I tend to keep things separate for code sanity.
For your javascript:
<script language="javascript">
function ClientValidate(source, arguments)
{
var date = new Date(arguments.Value); //make sure it's something javascript can parse
//validate age here
arguments.IsValid = isValid(date);
}
</script>
I would like to do this :
<asp:ImageButton runat="server" ID="addToCartIMG" OnCommand="btnAdd_Click" EnableViewState="false" CommandArgument='<%# itemId1.Value + ";" + Name1.Text %>' ImageUrl="<%$Resources:MasterPage, Image_AddToCart%>" />
where Item1 is hiddenField and Name1 a literal.
When I debug the method btnAdd_Click, the CommandEventArgs is empty and I don't understand why...
Thank you
You can't evaluate form field's values as you do in your example. If these values you try to evaluate are static, I mean doesn't change by your user's actions, you can pass them statically to your button's CommandArgument property.
If the values are changing by your user's actions, then you should get them at server-side by the reference of your controls like that :
string itemId = itemId1.Value;
// OR :
string itemId2 = Request.Forms["itemId1"];
For LiteralControl, you can't get it's text. you should turn it to form element.
Eval method is not a client-side function that passes your controls' values dynamically to server.