1

I am working on a team site collection inside my on-premises farm 2013. and i created a site column of type Currency which have "Number of decimal places" = 2, as follow:-

enter image description here

but when i try adding or editing items using the built-in list forms, i can enter extra decimal places inside the above site column as follow, without getting any warning or any error:-

enter image description here

the only thing the "Number of decimal places" will have effect on, it that when i save the item, the system will trim extra decimal numbers by doing some rounding. currently if i enter 10.12345 it will be saved as 10.12, while if i enter 5.7999 it will be saved as 5.80. so is there a way i can prevent users from adding more than 2 decimal places ??

2

That is how the number of decimal spaces works. You could use a JSLink to apply validation (causing a notification that more than 2 decimals is not allowed).

This should show an error on clicking save and wait for acceptable input... Totally untested, but it's where I'd start.

The regex allows $ (change to symbol of your choice) but does not require, allows commas every 3 digits (not required), allows 2 decimals (or none, or one). There may be some validations that are valid but missed (I found the regex at RegexLib). This could be changed to look at keystrokes and validate as typing occurs, but I'll leave that for someone else.

Save this file somewhere (I use a folder in _catalogs/masterpage), check it in and on the form settings link with JSLink.


// List add and edit - Decimal Place Validation

(function () { 

    // Create object that have the context information about the field that we want to change it's output render  
    var monthlyCostFieldContext = {}; 
    monthlyCostFieldContext.Templates = {}; 
    monthlyCostFieldContext.Templates.Fields = { 
        // Apply the new rendering for Monthly Recurring Cost field on New and Edit Forms 
        "Monthly Recurring Cost": { 
            "NewForm": monthlyCostFieldTemplate, 
            "EditForm":  monthlyCostFieldTemplate 
        } 
    }; 

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(monthlyCostFieldContext); 

})(); 

// This function provides the rendering logic 
function monthlyCostFieldTemplate(ctx) { 

    var formCtx = SPClientTemplates.Utility.GetFormContextForCurrentField(ctx); 

    // Register a callback just before submit. 
    formCtx.registerGetValueCallback(formCtx.fieldName, function () { 
        return document.getElementById('inpCost').value; 
    }); 

    //Create container for various validations 
    var validators = new SPClientForms.ClientValidation.ValidatorSet(); 
    validators.RegisterValidator(new decimalValidator()); 

    // Validation failure handler. 
    formCtx.registerValidationErrorCallback(formCtx.fieldName, decimalOnError); 

    formCtx.registerClientValidator(formCtx.fieldName, validators); 

    return " \ 
            
"; } // Custom validation object to validate email format decimalValidator = function () { decimalValidator.prototype.Validate = function (value) { var isError = false; var errorMessage = ""; //decimal format Regex expression var decimalRegex = /^(\$|)([1-9]\d{0,2}(\,\d{3})*|([1-9]\d*))(\.\d{2})?$/; if (!decimalRegex.test(value.toString()) && value.toString().trim()) { isError = true; errorMessage = "Invalid amount entered"; } //Send error message to error callback function (decimalOnError) return new SPClientForms.ClientValidation.ValidationResult(isError, errorMessage); }; }; // Add error message to spnError element under the input field element function decimalOnError(error) { document.getElementById("spnError").innerHTML = "" + error.errorMessage + ""; }

A simpler idea is to put a description on the column that will notify users that anything beyond 2 decimals will be rounded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.