Skip to main content
Known Participant
May 4, 2024
Answered

How to validate the form field

  • May 4, 2024
  • 3 replies
  • 2724 views

Hi All, Good day 

I have multiple forms and I have to validate the character limit used for the State field. It should allow only 2 character. If the character limit not set or if the character limit set more that 2 it should highlight the field. 

Below is the script I am try to accomplish the above task. and it is not working properly. 

 

var arr = ['Rep_state','clm_st','ee_st','ee_st2','patient_st','physician_state','wc_st','phys_st1','phys_st2']; //Filed names
for (var i = 0; i < this.numFields; i++) {
var A = this.getField(this.getNthFieldName(i));
if (arr.indexOf(A.name) >=0) {
if(A.name.charLimit != 2){
app.alert("Check the state character Limit") // Or it should highlight the field. 
}
}
}

This topic has been closed for replies.
Correct answer try67

Hi All, 

I able to set the alert message for the field which is not set in "Date format", However each time i have change the date in the script for current date is there a way to auto update the script to current date, Also if the field is incorrect as per the validatioin script is it possible to highlight or change the fill or stroke color of the field. Below is the script for reference. 

var fieldNames = ['aapp_date','aapp_date2','actual_del_date','actual_delivery_date','aeff_date','aeff_date2','ail_date1','ail_date2','ail_date3','bapp_date','bapp_date2','basic_date','bdate_yg','beff_date','beff_date2','capp_date','capp_date2','ceff_date','ceff_date2','clm_bdate'];
var incorrectFields = [];
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var field = this.getField(fieldName);

if (fieldNames.indexOf(fieldName) >= 0) {
if (field.value !== "05/26/2024") {
incorrectFields.push(fieldName);}}}

if (incorrectFields.length > 0) {
app.alert("Check the date format for the following fields:\n" + incorrectFields.join("\n"));}

 


You can use this:

 

if (field.valueAsString !== util.printd("mm/dd/yyyy", new Date())) {
	incorrectFields.push(fieldName);
	field.fillColor = color.red;
} else field.fillColor = color.transparent;

3 replies

Nesa Nurani
Community Expert
May 4, 2024

This will print all fields name from array in an alert that doesn't have character limit set to 2:

var fieldNames = ['Rep_state','clm_st','ee_st','ee_st2','patient_st','physician_state','wc_st','phys_st1','phys_st2'];
var incorrectFields = [];
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var field = this.getField(fieldName);

if (fieldNames.indexOf(fieldName) >= 0) {
if (field.charLimit !== 2) {
incorrectFields.push(fieldName);}}}

if (incorrectFields.length > 0) {
app.alert("Check the state character limit for the following fields:\n" + incorrectFields.join("\n"));}

 

Known Participant
May 4, 2024

Hi Nesa, 

This what i wanted to do for validation. Thank you so much for the code. it worked!!!

Bernd Alheit
Community Expert
May 4, 2024

Want you validate the names of the fields or the values?

try67
Community Expert
May 4, 2024

A better way is to print out the field name, instead of a cryptic alert window that doesn't even specify which field it's about... Or better yet, just set the charLimit value directly in your code.

Known Participant
May 4, 2024

Hi try67, The problem I am facing is even if we set the charlimit, the app alert shows the alert message for all the field which has been set for character limit  2, I thing I am missing some thing in the code. 

Also how can we print the Field name if it is not set for 2, Note we have multiple fields for the state in the array. 

 

 

try67
Community Expert
May 4, 2024

The problem with your code (which I only noticed now) is that you're checking the charLimit property of A.name, instead of A...

Anyway, this code will set all the fields in your array to a 2-char limit:

 

var arr = ['Rep_state', 'clm_st', 'ee_st', 'ee_st2', 'patient_st', 'physician_state', 'wc_st', 'phys_st1', 'phys_st2']; //Filed names
for (var i = 0; i < arr.length; i++) {
    var A = this.getField(arr[i]);
    A.charLimit = 2;
}

 

Just be aware the reverse is not possible. Once set, you can't reset them back to zero (using a script, you can do it manually, of course).