MaskedEditExtender, dates and Globalization - asp.net

I want to use the MaskedEditExtender to mask short dates. The problem is that I want to mask the field depending on the user language settings.
This is working for a lot of cases, but for example for Latvian Culture (with format 9999.99.99. ) is not working.
<cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" AutoComplete="True" MaskType="Date" TargetControlID="myTextbox" ClearMaskOnLostFocus="True"
OnInvalidCssClass="myInvalidCss" OnFocusCssClass="myOnFocusClass" Mask="99/99/9999" >
</cc1:MaskedEditExtender>
Is there a simple way to set the Mask property with the user culture mask format?
Am I missing something to do this easier?

Not sure why the extender won't recognize Latvian culture, but try looking at the overrides provided, such as CultureDateFormat and CultureDecimalPlaceholder. More info at the AJAX Control Toolkit sample website.
EDIT: Response to OP's comments:
I have not idea if this works, but it looks like you can get the short date format for a culture from the CultureInfo class, like this.
string shortDateFormat =
System.Globalization.CultureInfo.DateTimeFormat.ShortDatePattern
Take a look here for examples.

Related

How to modify currency format using DataFormatString in GridView

How would I display the value, for example:
851839.850000
To show its currency equivalent with comma and period formatting:
£851,839.85
by using DataFormatString? Currently I have £{0:c2}, but clearly this is not enough, as it gives me
£851839.850000
It turned out that the original value that was being bound was actually in a string format. Simpling making sure it was incoming as a Decimal allowed the formatting:
£{0:C}
to work precisely as expected and format into a correct decimal value with a £(British Sterling) to be placed before the value.
Define culture like
<%# Page Language="C#" MasterPageFile="~/MasterPage.master"
CodeFile="Search.aspx.cs" Inherits="Search_aspx" Title="Search"
UICulture="hi-IN" Culture="hi-IN" %>
Find culture code from this list
You could try £{0:N2}, see if that works.
Currency formatting is based on the current NumberFormatInfo. C2 ought to work for you, depending on your current culture, but since it doesn't you might want to look at the NumberFormatInfo information.
There's more about currency formatting here: The Currency ("C") Format Specifier
TO display in text box..Try
textbox.text = Format(Value,"$#,###.00")

how to force TextBox to only accept strings

Sorry for the Dummy Question , i know :( ,, but it's only the simple things that dont work with me :((
i have many text boxes and i want the user to only insert String and not Numeric numbers ,
how could i handle it in easy way ??
as it takes every thing and apply it to the database , or should i control it from the Database
PS. i have searched a lot but with no good answer
use [a-zA-Z]+ for ValidationExpression:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="RegularExpressionValidator"
ValidationExpression="[a-zA-Z]+"></asp:RegularExpressionValidator>
You could take a look at validation techniques for asp : http://msdn.microsoft.com/en-us/library/7kh55542.aspx
This provide a set of tools to check whether the input match what you expect.
You can do it easily in AJAX,just download it from here
first add a script manager to your page then add FilteredTextBoxExtender to the textbox and set it's properties as you wish.
A Regular Expression could be applied to the input
For the basics on RegEx : http://www.regular-expressions.info/tutorial.html
And also see
http://www.regular-expressions.info/dotnet.html
You can use regex to do that with jQuery.
In this example, I replace only digits.
You can adapt the regex to replace any set of characters with an empty string.

ASP.NET compare end time and start time with customvalidator

I have two text boxs for time input. I'm trying to compare two time with format : 10:14:23 AM. Then End-TIme must be greater or equal to Start-Time. The ASP.NET CompareValidator doesnt offer the time type, so I'm thinking of using the Customvalidator. I never use Customvalidator to compare the two text boxs for time input before. How to compare the two time? Thank you.
Have you looked at the MSDN entry for the CustomValidator? It specifies usage and gives an example of how to use it:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx
<asp:CompareValidator
ID="ClosedTime_CompareValidator"
runat="server"
ControlToCompare="OpenedTimeTextBox"
ControlToValidate="ClosedTimeTextBox"
ValidationGroup="Submit"
Operator="GreaterThanEqual"
Display="Dynamic">
<img
src="./FormImages/warning.gif"
alt="The entry is not recognizabledate."/>

Displaying proper date format depending on culture

I am using a control for a popup calendar date picker. This uses a javascript function, SetText, to set the textbox to the given date. I can't change anything in the calendar control itself but I can override the SetText function. The SetText javascript just takes the TextBox name and the date value in string format and sets the TextBox to the string.
The problem:
I need to display the date in the format "April 30".
Easy to do. Use getMonth() and getDate() where I can parse the information from there.
Now, I need to make sure this shows correctly for different cultures. For example, the UK shows dates as "30 April". Since the code-behind(c#) could be sending the date in the UK format how do I know in the javascript that they're using UK(dd/mm/yyyy) and not US(mm/dd/yyyy)?
The browsers navigator language could be set to one setting while the server is set to another to cause a January 4 as April 1 mismatch.
You are using the Microsoft Ajax Framework, this framework defines a set of "client-side type extensions" which provide added functions or "extensions" to the JavaScript base types.
The Date Type Extensions is what you're looking for, specifically the Date.parseLocale function.
With this function you can parse a string, using a given format.
You can synchronize your server-side and client-side culture by setting the ScriptManager.EnableScriptGlobalization property to true, and use the Date.parseLocale function without specifying any format.
Give a look to this article:
Walkthrough: Globalizing a Date by Using Client Script
See toLocaleString and related functions.
If you control the backend, why not just send a timestamp and push it into Date object?
As for formatting on the client side, since I was already using Dojo, I solved this problem by using dojo.date.locale.format. It was completely painless.
Locale is detected automatically or can be set arbitrarily.
Shorthand format options (e.g.: long short)
Data selectors (e.g.: time, date)
Ability to specify an arbitrary date/time pattern (probably not application to this application, but still useful).
Tutorial: http://docs.dojocampus.org/dojo/date/locale
API doc:
http://api.dojotoolkit.org/jsdoc/1.3/dojo.date.locale.format
Date format descriptions: http://www.unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns
Three things you could use:
1) toLocaleString - As suggested already. The problem with this is when sending a string of "4/1/2009" this can result in a couple things. January 4 or April 1.
2) navigator.language and navigator.systemLanguage - After you get the date string you can check to see what language the system is in and parse the date from there. The problem with this and solution 1 is what if you have a UK server and the browsers machine is US. You will have the code behind sending April 1 as 1/4/2009 where the javascript will read the string as whatever language the clients browsers is. So, UK server and US browser will give you a wrong result.
3) Use Code Behinds Culture - Create a variable in your javascript that when the page loads, it will call a function in your code behind that returns this.Page.Culture from there, you will know what culture the string is being sent back as. This will eliminate the mismatch that the first two solutions can cause. It will take a little extra work to make sure it's displayed correctly but at least you will be able to use the string without having the possibility of mismatching cultures.
toLocaleDateString would be a better solution than toLocaleString for your problem as it doesn't include the time (as you only are requesting the date).
The open-source JavaScript library Date.js has some great methods for formatting dates, as well as it supports a bunch of languages:
Date.js at Google Code: http://code.google.com/p/datejs/
If you want nicely formatted dates / times, you can just pass a formatting string (nearly identical to those used in .NET Framework) into any Date object's .toString() method.
It also has a whole set of cultures which allow you to simply include the appropriate script for that culture.
If you want to manage that yourself (as we do in our apps), you can find resources which give you the list of appropriate resource strings for a given culture. Here's one that shows proper formatting strings for a ton of cultures: http://www.transactor.com/misc/ranges.html
As you are using ASP.NET then you may also be using ASP.NET Ajax. If so, there are two properties on the ScriptManager that are of use to you:
EnableScriptLocalization - Gets or sets a value that indicates whether the ScriptManager control renders localized versions of script files.
EnableScriptGlobalization - Gets or sets a value that indicates whether the ScriptManager control renders script that supports parsing and formatting of culture-specific information.
<asp:ScriptManager ID="AjaxManager" runat="Server" EnablePartialRendering="true"
EnableScriptGlobalization="true" EnableScriptLocalization="true" />
When you enable both of these (set to true) then ASP.NET Ajax extenders etc. should automatically be localised into the culture specified in web.config:
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB" />
</system.web>
</configuration>
For instance, setting this will localise the AjaxControlToolkit Calendar into your specificed culture.
Even if you are NOT using ASP.NET Ajax adding a ScriptManager and enabling localisation will give you a useful javascript variable called __cultureInfo that contains a JSON array of localised formate, such as currencies, dates etc.
"CalendarType":1,"Eras":[1],"TwoDigitYearMax":2029,"IsReadOnly":true},"DateSeparator":"/","FirstDayOfWeek":1,"CalendarWeekRule":0,"FullDateTimePattern":"dd MMMM yyyy HH:mm:ss","LongDatePattern":"dd MMMM yyyy","LongTimePattern":"HH:mm:ss","MonthDayPattern":"dd MMMM","PMDesignator":"PM","RFC1123Pattern":"ddd, dd MMM yyyy HH\u0027:\u0027mm\u0027:\u0027ss etc....
I solved this problem by using Datejs as
In codebehind(aspx.cs) I get the culture for an employee and add the appropriate js to the header as
string path =
"http://datejs.googlecode.com/svn/trunk/build/date-"
+ GetCulture() + ".js"; Helper.AddJavaScript(this, path);
(in your case you can get the culture from navigator.systemLanguage (or navigator.browserLanguge etc) and add a script tag to the header with src attribute pointing to the appropriate path)
On the client-side I use
d.toString(Date.CultureInfo.formatPatterns.shortDate)
where d is any date object
(I tried using Date.today().toShortDateString() but it was throwing exception. (the CultureInfo JSON object had a different structure than what the function expects).

ASP.net MaskedEdit: Entering number 425.25 produces 4250000000.25. How can I fix this?

I had a NUMBER field in an ORACLE Database that is set to 13,2. I want to use the MaskedEdit field in order to mask this.
If I enter 425.25, it produces a 4250000000000.25, instead of moving the 425 over. I'm literally entering 425 pressing the period key and then 25, which moves me into the decimal area of the MaskedEdit. That works great, but I need the main integer to move down and not fill with zeros.
Any ideas?
It could be a localized version of your software that is misinterpreting the character . (period). Please try a , (comma) in substitution to the period.
Additionally you should check the CultureInfo, which is an important tool to prevent errors like this, you should set it on your Web.Config and every method that accepts it, like MS Code Analysis tells you so. Common methods that accepts a CultureInfo parameter are:
Parse (int.Parse, double.Parse, ...)
ToString (int.ToString, double.ToString, ...)
Check your Mask property:
This is not the correct markup, just an example from Ajax Control Toolkit site.
<ajaxToolkit:MaskedEditExtender
TargetControlID="TextBox2"
Mask="9,999,999.99"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
MaskType="Number"
InputDirection="RightToLeft"
AcceptNegative="Left"
DisplayMoney="Left"
ErrorTooltipEnabled="True"/>
Dropped the MaskedEdit control and used a JQuery plugin to produce the same effect, only not flawed.

Resources