1

I have a problem using jquery.validate on my asp.net mvc 3 app.

At least in Spain we use the "," to split a number from its decimals. Ok, using server side validation, if I put something like:

12.55 when the server validate it, it says that the value is not valid. If I put: 12,55 it works.

So far so good, but if I use jQuery validate, it says that 12,55 is invalid and 12.55 is the valid one. So, the client pass the validation but the server not, or the client doesnt pass but the server would.

So... can I change the locale for that kind of validation?

Thank you.

PS: How the server knows that I want to validate using "," and not "."? I don't remember if I specified that somewhere.

2 Answers 2

1

Probably you should include localization files:

http://ajax.aspnetcdn.com/ajax/jQuery.Validate/1.7/localization/messages_es.js

http://ajax.aspnetcdn.com/ajax/jQuery.Validate/1.7/localization/methods_de.js

In Germany one uses the same rules for numbers, so you can use methods_de.js or just include

jQuery.extend(jQuery.validator.methods, {
    number: function(value, element) {
        return this.optional(element) ||
               /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});

Here the list of files hosted by Microsoft CDN for the version of 1.6. The version 1.7 has the same files.

UPDATED: See demo here.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried with methods_de but a number like "40,5" is below (0.01) so the Range rule fire up. I don't know why it thinks that is below. Well, is a step forward.
@Jesus Rodriguez: To help you I prepared the demo ok-soft-gmbh.com/jQuery/validateDeNumber.htm. Is it what you want?
@Jesus Rodriguez: If you set the rules on the server (like Range) you should examine how it looks on the client to find out the problem, But I suppose that on the server you should place Range and so on in US format.
Oh, it seems that the Range attribute doesn't like that regexp, but is working perfect without it, thanks.
0

You could try configuring your web application to use the locale of the client web browser:

<system.web>
    <globalization requestEncoding="utf-8" 
                   responseEncoding="utf-8"
                   culture="auto" 
                   uiCulture="auto" />
    ...
</system.web>

This will use the browser culture for server side validation. As far as the client side validation is concerned it already uses the browser culture. So this ensures that both match.

1 Comment

Not working for me. Googling I see that some people is using some kind of jquery.glob.js and other are disabling unobtrusive javascript. I tried with the globalization thing but doesn't work for me. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.