Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - client validation using jQuery validate for currency fields

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...