[RESOLVED]How can i validate user input is in correct locale ?

Hi,

I have set my regional settings to France (fr-FR) and the number would be for example 3,11 . 

How can i make it so that if the user enters 3.11 (decimal)  in a textbox then an error is popped up to say that the number format is incorrect .

thanks

Salut, you can use ControlValidators.

For example 

<asp:textbox id="textbox1" runat="server"/>
<asp:RangeValidator id="valRange" runat="server"
ControlToValidate="textbox1" Type="Double" 
ErrorMessage="Wrong format!" Display="static">*</asp:RangeValidator>

where Type could be also set as  "Currency", depends on your requirements.

Also make sure you set proper culture in the web.config file

<globalization culture="fr-FR" uiCulture="fr-FR" />

Hi,

thanks for the reply , but is there an example using <asp:RangeValidator> which validates on the current browser culture , as i cant see how it will exactly work.

I can get the culture in javascript, so i was thinking could i call onblur and validate the input value ?

thanks

What about the example above, doesn’t it clear enough? There is a lot of documentation as well as samples on the internet regarding this matter.

As in case of javascript you most likely need to replace "," by a "." and check if it is a decimal number 

e.g. using jquery

$.validator.methods.range = function (value, element, param) {
    var globalizedValue = value.replace(",", ".");
    return this.optional(element) || (globalizedValue >= param[0] && globalizedValue <= param[1]);
}
 
$.validator.methods.number = function (value, element) {
    return this.optional(element) || /^-?(?:d+|d{1,3}(?:[s.,]d{3})+)(?:[.,]d+)?$/.test(value);
}

Source: http://rebuildall.umbraworks.net/rebuildall/2011/03/02/jQuery_validate_and_the_comma_decimal_separator 

or using javascript (e.g. using parseFloat())

function isNumber(n) {
n = n.replace(",", "."); return !isNaN(parseFloat(n)) && isFinite(n); }

Source: http://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric 

It’s also possible to validate currency (fixed number of digits after comma)

http://stackoverflow.com/questions/18480658/decimal-validation-in-the-textfield-using-javascript-or-jquery 

Thanks for that . 

Leave a Reply