Using Condition String field type

lberthold
Kilo Contributor

Hello -

I wanted to create a custom string field on the Contract table that would follow a specific syntax...for example, the number of characters in the field should equal 8, and the first two should start with FA, while the remaining six characters should be numbers (e.g., FA123456).

Could I use a Condition String field type to achieve this? I'm just not sure how to validate the syntax though...

Any ideas?

Thansk!

Loir

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Lori,



You would just use a string field here, but then apply a Client Script to that field to enforce the specific syntax with a regular expression (regex). I have an example that checks that the appropriate number of characters have been entered, and formats appropriately or lets the user know if they did not input properly. I've made a slight modification here to match your description. What it essentially does is checks for the format you want. If it does not match, is strips out all the non-digits to see if there are 6 digits. If there are, then it puts them in the appropriate format with FA at the front. If not, it posts a message letting the user know what format to enter. You would need to replace FIELDNAME with the field you are using. I think this will do what you are looking for.



Ben



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



  var testString = '';


  testString = g_form.getValue('FIELDNAME);


  var testFieldFormat = /^FA[0-9]{6}$/; // Pattern for appropriate format



  // Check for match to format, if it does not match, strip non-digit characters and apply format


  if(!testFieldFormat.test(testString)){


  testString = testString.replace(/[^\d]/g,'');


  var testDigitsPattern = /^[0-9]{6}$/; // Determine if six numbers entered



  if(!testDigitsPattern.test(testString)){


  // g_form.clearValue('FIELDNAME');   // Uncomment this if you want to clear the value


  g_form.showFieldMsg('FIELDNAME', "Invalid Field Value.   Please enter the 6 digit FA code in the form FA######",'error');


  }


  if(testDigitsPattern.test(testString)){


  g_form.hideAllFieldMsgs();


  newNum = 'FA' + testString;


  g_form.setValue('FIELDNAME',newNum);


  }


  }



  if(testFieldFormat.test(testString)){


  g_form.hideAllFieldMsgs();


  }


}


View solution in original post

6 REPLIES 6

Hi Ben



Do you have any idea how can we make a string of maximum length 7 and having characters , numbers and symbols also.??


Community Alums
Not applicable

Hi Sumit,



That would involve a few modifications to the regular expressions used in the script above. It would involve removing the fixed format check (unless you want to have a fixed format that is checked first).


You would also not want to strip non-digit characters, adjust the search for all acceptable characters, and set it for a maximum of 7 characters. I know how to get it to check for exactly 7, but would need to check online for how to look for a maximum of 7 instead.



Ben