0

I'm here now because of two things.

First:

I need to modify a jQuery script to fix that decimal separator (dot or coma). In my country (Argentina), we use the coma as the decimal separator, and jQuery.validation uses the dot. I managed myself to change the RegEx and is now fixed, but my question come from other side.

Whenever I add a View, it references two scripts

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

Since I've modified directly the jquery.validate.js only, how can I refactorize this into the min (I don't know the differences between them, so a little bit of water at this point would be great). ¿It would be better to extend the script? ¿How? (Tried this LenardG's jQuery validate and the comma decimal separator (MVC) but didn't work :( )

I also don't know if I can change one by another just like that and it will keep working.

Second:

I need to globalize the messages from the jQuery client validation.

For example, when you type a letter in a numeric field (lets say age) it displays a message saying "The field age must be a number."... I've been driving myself crazy looking up the entire solution, over and over for that message (IMHO, work of the devil). But obviously, didn't succeed.

Although the good practices would be selecting the culture and that stuff according to the web browser culture, the web application that I have to do has to let the user of the system be the one who selects the culture (maybe in login page or in a user configuration page, but this doesn't matter).

So, if the user selects the es-AR culture (spanish from Argentina), the message that I wanna show is "El campo edad debe ser un número."; if the user selects the fr-FR culture, the message to show should be (thanks Google translate) "Le champ âge doit être un nombre."


Sorry about my english, I hope you can understand my "questions". Thanks people ;)

3 Answers 3

1

Please, just add this code after the jquery.validate scripts:

$(document).ready(function () {
    $.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);
    }
    $.validator.methods.date = function (value, element) {
        var isValid = true;
        if (Object.prototype.toString.call(d) === "[object Date]") {            
            if ( isNaN( d.getTime() ) ) { 
                isValid = false;
            }            
        }
        else {
            isValid = false;
        }
        return this.optional(element) || isValid;
    }
});

This will put the date accordingly to the navigator culture and uses "," as decimal separator.

Regards.

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

Comments

0

I'm coding a contrib project for MVC3 which includes an easier way to handle localization of messages: http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

1 Comment

It's very easy and straightforward, even though I prefer using simple resource files created by myself and changing by hand all of the messages.
0

First off, I believe you only need one of those validation scripts included (if they're just regular and minified).

Create another js file called app.domready.js This will be where you put regular production code. It's also typically not good to directly edit a plugin for updating purposes.

Put all this in app.domready.js and reference it in your masterpage/view _Layout.cshtml.

<script src="@Url.Content("~/Scripts/app.domready.js")"></script>

This will initialize the jquery.validate plugin.

$(document).ready(
      function () {         
                $('form').validate({
                rules: {
                firstname: "required",
                lastname: "required",
                username: {
                    required: true,
                    minlength: 2
                },
                password: {
                    required: true,
                    minlength: 5
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                },
                email: {
                    required: true,
                    email: true
                },
                topic: {
                    required: "#newsletter:checked",
                    minlength: 2
                },
                agree: "required"
            },
            messages: {
                firstname: "Please enter your firstname",
                lastname: "Please enter your lastname",
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                },
                email: "Please enter a valid email address",
                agree: "Please accept our policy"
            }

    });
});

You set your parameters in there based on the form ID/class(s). Check out the source code at http://jquery.bassistance.de/validate/demo/ for help.

And min or any jquery plugin that has .min in the name means minified or compressed to save white-space/size when the browser parses it which means less for your user to download which means faster. We typically use uncompressed in development and have a build script in VS2010 to compress everything.

Hopefully I understood what you're trying to do.

Comments

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.