Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

validate fields in the form in click event

Avatar

Level 5

Hi All.

In my form I validate DateField for date format and EmailField for email address. Those fields are validating when user input wrong value. I would like to execute validation of those fields when user fill in all fields of the form and click event of button. My code for button in click event:

if (form1.page1.email.rawValue !== null || form1.page1.Date1.rawValue !== null) {

if (form1.page1.email.execValidate() == true) {

if (form1.page1.Date1.execValidate() == true) {

xfa.host.messageBox("Your form has passed validation and is now ready to save.");

    app.execMenuItem("SaveAs");

}

}

}

else

{

form1.page1.email.ui.oneOfChild.border.fill.color.value = "255,225,225";

form1.page1.Date1.ui.oneOfChild.border.fill.color.value = "255,225,225";

xfa.host.messageBox("Please fill out the required fields first.");

}

But unfortunately it not works like I expected. How to fix the problem?

Thanks.

6 Replies

Avatar

Level 7

I don't know if this will fix it but line 01 has an error.

if (form1.page1.email.rawValue !== null || form1.page1.Date1.rawValue !== null) {

Should be

if (form1.page1.email.rawValue != null || form1.page1.Date1.rawValue != null) {

You have  too many = before null. Not null is simply != null

Avatar

Level 5

I fixed like you suggested but it didn't solve the problem.

My email field validate event:

if ( this.rawValue != null && this.rawValue != "")

{

var r = new RegExp("^[a-zA-Z0-9_\\-\\.]+\\@[a-z0-9_\\-\\.]+\\.[a-z]{2,3}$"); // Create a new Regular Expression Object.

// Set the regular expression to look for  an email address in general form.

var result = r.test(this.rawValue); // Test the rawValue of the current object to see

// if it fits the general form of an email address.

if (result == true) { // If it fits the general form,

form1.page1.subForm.subNamePhone.email.ui.oneOfChild.border.fill.color.value = "255,255,255";

true; // all is well.

}

else

{

xfa.host.messageBox("Please enter valid email address.e.g. abc@yash.com");

form1.page1.subForm.subNamePhone.email.ui.oneOfChild.border.fill.color.value = "255,225,225";

this.rawValue = null;

xfa.host.setFocus(this);

false;

}

}

and Date1 field validate event

var newDate = this.rawValue; 

if (newDate !== null) // process non-empty string 

{  

var isDate = false; 

var oMyDate = util.scand("mmddyyyy", newDate); 

if (oMyDate !== null)

this.rawValue = util.printd("mm/dd/yyyy", oMyDate); // strict format, adding slashes 

isDate = true;

form1.page1.subForm.subNamePhone.Date1.ui.oneOfChild.border.fill.color.value = "255,255,255";

 

if (isDate === false)  // Stay in field if invalid

{

form1.page1.subForm.subNamePhone.Date1.ui.oneOfChild.border.fill.color.value = "255,225,225";

app.alert("Invalid date entered -- please enter in MMDDYYYY format, \n\nfor example, 05192016 (no slashes required)", 0, 1, "CHECK DATE FORMAT"); // check validity 

this.rawValue = ""; 

xfa.host.setFocus(this)(); 

And I would like to validate those fields only when user fill out rest of the form fields and will click button.

How to do that?

Thanks.

Avatar

Level 7

These lines still have errors in the Date1 field validate event - use !=

if (newDate !== null)

if (oMyDate !== null)

I havent used field validation before

Remember to use the javascript console - CTRL+J when running the form and you can see any errors that might occur.

Avatar

Level 10

Hi MinusZero,

just to let you know, != or !== is accepted. It is just a matter of comparing, with or without converting, variables.

Avatar

Level 10

Hi there,

Unfortunately execValidate() only execute the events, it will return true only if the event has been executed.

you might want to verify this thread, Check if field validation passed (isValid?)

maybe you are using the wrong approach to do this, otherwise there is the validationState which you might want to consider to use instead of validate. I never really had to use validate nor validationState event, I always validate values on exit event or using patterns.

I hope this will help.