locked
Define max. length of an integer field in CRM 2011 RRS feed

  • Question

  • Hello Forum

    If I'd like to have a max. length on a number field how to do this? Do I really need JS?

    Thanks for help.

    Kind regards,

    Mirko

    Monday, December 17, 2012 1:42 PM

Answers

  • Jason is right that it is a SQL limitation on the int length, and that a text field would probably be your best bet, which would require javascript validation on the field to only allow numbers.

    Note that by using a text field you will lose any sorting or filtering ability in advanced find/views.

    Here is some sample JavaScript for validating a text field for numbers in CRM (note this is using the Account Number field as an example).

    Add the following function calls/event handlers to your form (to the account form in this case):
    OnLoad: captureAccountNumber
    Account Number OnChange: validateAccountNumber

    //remember the previous value for rollback
    var _accountNumber = null;
    function captureAccountNumber() {
        _accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();
    }
    
    //make sure the account number is actually a number
    function validateAccountNumber() {
        var accountNumberField = Xrm.Page.getAttribute("accountnumber")
        var accountNumber = accountNumberField.getValue();
        if(!isNaN(parseFloat(accountNumber)) && isFinite(accountNumber)) //<--can use any number validation here
        {
            //it's a number, set the new 'previous value'
            captureAccountNumber();
        }
        else {
            //it's not a number, reset previous value and set focus to field
            alert("You must enter a valid number.");
            accountNumberField.setValue(_accountNumber);
            Xrm.Page.getControl("accountnumber").setFocus();
        }
    }

    Hope that helps

    Paul

    • Marked as answer by CRMBE Tuesday, December 18, 2012 9:12 AM
    Tuesday, December 18, 2012 5:40 AM

All replies

  • If you go into the field properties, you can set the Maximum Value (and Minimum Value) of a Whole Number field. This value will be enforced be the system without any scripting. 

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, December 17, 2012 2:10 PM
    Moderator
  • Thanks Jason. I know that, but what you do when you need 20 for ex.? I allows max. 2.471.xxx.xxx - so it's not working with 99999999999999999999
    Monday, December 17, 2012 4:13 PM
  • If you need to go outside the limitations of the min/max values CRM provides I think you would need to use a text field instead and enforce values with JavaScript. I don't think it is as much a limitation in CRM as opposed to a datatype limitation with SQL Server. 

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Monday, December 17, 2012 4:42 PM
    Moderator
  • Jason is right that it is a SQL limitation on the int length, and that a text field would probably be your best bet, which would require javascript validation on the field to only allow numbers.

    Note that by using a text field you will lose any sorting or filtering ability in advanced find/views.

    Here is some sample JavaScript for validating a text field for numbers in CRM (note this is using the Account Number field as an example).

    Add the following function calls/event handlers to your form (to the account form in this case):
    OnLoad: captureAccountNumber
    Account Number OnChange: validateAccountNumber

    //remember the previous value for rollback
    var _accountNumber = null;
    function captureAccountNumber() {
        _accountNumber = Xrm.Page.getAttribute("accountnumber").getValue();
    }
    
    //make sure the account number is actually a number
    function validateAccountNumber() {
        var accountNumberField = Xrm.Page.getAttribute("accountnumber")
        var accountNumber = accountNumberField.getValue();
        if(!isNaN(parseFloat(accountNumber)) && isFinite(accountNumber)) //<--can use any number validation here
        {
            //it's a number, set the new 'previous value'
            captureAccountNumber();
        }
        else {
            //it's not a number, reset previous value and set focus to field
            alert("You must enter a valid number.");
            accountNumberField.setValue(_accountNumber);
            Xrm.Page.getControl("accountnumber").setFocus();
        }
    }

    Hope that helps

    Paul

    • Marked as answer by CRMBE Tuesday, December 18, 2012 9:12 AM
    Tuesday, December 18, 2012 5:40 AM
  • Thanks a lot!
    Tuesday, December 18, 2012 9:12 AM