• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
7

How to validate the form field

Participant ,
May 03, 2024 May 03, 2024

Copy link to clipboard

Copied

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. 
}
}
}

TOPICS
JavaScript , PDF , PDF forms

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
3 ACCEPTED SOLUTIONS
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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"));}

 

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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).

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

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;

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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. 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

Thank you so much Try67, Much appreciated! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

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"));}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

Hi Nesa, 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

Hi Nesa,

 

The same concept I have used to validate the date formatted fields, However nothing happening. 

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','consult_date1','consult_date2','dapp_date','dapp_date2','date_admitted','date_discharged','date_how_long','date_returned_full','date_returned_part','deff_date','deff_date2','eapp_date','eapp_date2','earn_eff_date','ee_emp_date','eeff_date','eeff_date2','exp_del_date','expected_delivery_date','first_visit_date','ill1_date','ins_eff_date2','last_cons_date1','last_cons_date2','last_inc_date','last_wk_date2','ltd_ee_date','patient_birthdate','recent_visit_date','returned_date','spouse_bdate','stop_work_date','surgery_date','symptons_date','term_date,unable_date','wc_inj_date','wk_cease_date','hosp_from1','hosp_thru1','emp_from1','emp_from2','emp_from3','emp_to1','emp_to2','emp_to3','patient_return','patient_returned'];
var incorrectFields = [];
for (var i = 0; i < this.numFields; i++) {
var fieldName = this.getNthFieldName(i);
var field = this.getField(fieldName);
var cFormat = "mm/dd/yyyy";
if (fieldNames.indexOf(fieldName) == cFormat) {
if (field.AFDate_FormatEx !== "cFormat")
if (field.AFDate_KeystrokeEx !== "cFormat") {
incorrectFields.push(fieldName);}}}

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

You could check the field date format once it's filled, but to check the actual format it's set to, don't think that is possible.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 04, 2024 May 04, 2024

Copy link to clipboard

Copied

Thank you Nesa, Understood.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 23, 2024 May 23, 2024

Copy link to clipboard

Copied

Hi Nesa,

 

Can you tell me how to validate the date field post filled. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 23, 2024 May 23, 2024

Copy link to clipboard

Copied

What do you want to validate? Check if the date is in correct format?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 23, 2024 May 23, 2024

Copy link to clipboard

Copied

I need to ensure that all fields representing dates are in the correct date format. If I forget to set a field in the date format, the JavaScript code should display an error message indicating that the field is not set in the correct date format. I have the capability to manually fill out the form and validate each field for different  formats. However, I would prefer to automate this process so that I can simply copy and paste the field name and perform the validation, similar to how I validate chart limits.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

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"));}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 28, 2024 May 28, 2024

Copy link to clipboard

Copied

LATEST

Hi try67, 

Thank you for the code it worked perfectly!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines