Wednesday, April 24, 2019

How to set Character Limitation in Field

Question:

How to point out and prevent users from putting more than (for example) 255 characters in text area field.

Answer:

Setting character limitation can be done with a client type script functions deployed on record(s) where you would like to perform this validation.

First function will be "Validate Field" function type and will point out user that his/her text has more than 255 characters when leaving the field. Second one will be "Save Record Function" type and will prevent record from saving when this field has still more than 255 characters. This method can be used also for other field types for example Free Form Text type.


Script Sample

Function clientValidateField(type, name){
  if (name == 'idOfYourCustomField'){
    var fieldLength = String(nlapiGetFieldValue('idOfYourCustomField')).length;
    if (fieldLength > 255){
      alert("Field SomeName has more than 255 characters.");
     return false;
    }
  }
  return true;
}

function saveRecord() {
  if (String(nlapiGetFieldValue('idOfYourCustomField')).length > 255){
    alert("Please reduce SomeName field to 255 characters");
    return false;
  }
  return true;
}

No comments:

Post a Comment