Expand my Community achievements bar.

Pop up appears when form is opened?

Avatar

Level 2
I am new to JavaScript and I have the following script that I hope someone can help me with.



I am using LiveCycle Designer 7.0 from Acrobat Professional. I am trying to validate the entry in a test field. I want to make sure that the four digit year is not less than the current year and can be a year in the future.



My problem is when the form is opened in Reader the alert box pops up before the actual form. When I key in the field the validation seems to be working. What do I need to do to stop the alert from showing when the form is first opened?



----- form1.Alabama.Yr::validate - (JavaScript, client) -------------------



var today = new Date();

var year = today.getFullYear();



if (this.rawValue === year || this.rawValue < year)



{



app.alert("Year is not correct.");

Yr.rawValue = null;



}



Much thanks in advance.
6 Replies

Avatar

Former Community Member
Maybe add the script to field's 'exit' event

Avatar

Former Community Member
Hi Nancy,



Validation scripts are useful and provide built-in capabilities you should be aware of.



First, the script is meant to validate the fields value. If the expression is true, the validation will succeed. If false, the validation will fails. If the validation fails, the message specified in Object palette > Value tab > Validation Script Message will be displayed.



Your script should look something like so:



var today = new Date();

var year = today.getFullYear();



(this.rawValue == year || this.rawValue == year+1)



If you check the Error check box beside the Validation Script Message, the user wont be able to submit the form until a valid year is provided. The message should let the user know what value is valid.



Youll notice the script does not reset the fields value if the validation fails. If you choose to do so, you should do it separately. I must say that some users prefer to leave the invalid value untouched. They might have typed 2006, out of habit, and fixing it is a quick as replacing 6 with 7. If you delete their value, they have to retype the entire year. Just a thought.



Regards,



Hélène

Adobe Systems Inc.

Avatar

Level 2
Hélène-



Thanks for the detail regarding validation script, but I still am having a problem with it.



I have tried what you told me (remove reset the value) the popup no longer appears when the form is opened. But the validation does not seen to be working. It is not working either in the validate or exit event. This is what I have now on that text field.



----- form1.#subform[0].Y::exit - (JavaScript, client) ---------------------------------------------



//Verify year is current or year+1

var today = new Date();

var year = today.getFullYear();



if (this.rawValue == year || this.rawValue == year+1)



{

app.alert("Year is invalid.\n\nOnly current or next year is valid.");

}



----- form1.#subform[0].Y::validate - (JavaScript, client) -----------------------------------------



//Final verification

ScriptObject.ValidateField(event.target, this);



This final verification script I have taken from the samples on the

LiveCycle Developer Center (Validating field content in a form hosted in Adobe Acrobat).



The alert only displays when 2007 or 2008 is entered. I am really confused now. I thought I had a handle on this with your explanation.



I do not know where to go now. The Object Pallet is setup as:



Field Tab

Allow Plain Text Only and Limit Length are checked with a Max Chars: 4. Edit Pattern: 99999



Value Tab

Type: User Entered - Required

Validation Pattern: 9999

Validation Script Message is checked - in Message box Invalid Year.



Binding Tab

Default Binding: Normal

Data Format: Plain Text



I am hoping that you can explain what I have wrong.



Thanks so much,

Confused Nancy

Avatar

Former Community Member
Hello Nancy,



Have you tried copying my script example in the validate event? I noticed in your example above that you modified the script and placed it in the exit event. There shouldn't be any app.alert in a validation script. Instead, type the message in the Object palette > Value tab > Validation Script Message box.



Your example above also shows a validation script calling a Script Object. If this is how you want to proceed, make sure the ValidateScript() function returns true or false.



Can you give this a try and let me know?



Thx

Hélène

Adobe Systems Inc.

Avatar

Level 2
Hello Hélène -



I can not believe that I made this so difficult for myself. By cutting and pasting what you had in your first response into validate all is working great. I think for my first form I am trying to hard to make everything validate on one click. I have removed the ScriptObject from the form and will continue to use what you have taught me.



Thank you for being so patient with me.



Not as confused Nancy