Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

validating for empty text field

Avatar

Former Community Member
Hi All !



I want to conditionally validate the presence of a text field based on a radio button i.e. it is a yes/no radio button, and if "yes" is clicked, then a text field has to have a value. To do so, I wrote the following script in the Script Editor for the text_field and chose the "validate" option of the "Show" dropdown :



(radio_field.rawValue == "1") || (radio_field.rawValue == "2" && !text_field.isNull && text_field.rawValue.length > 1)



It does not work for the case where "yes" is clicked and text_field is empty. In that case, there is no error message. How can I validate that text_field is not empty when "yes" is clicked ?



Thanks,



Martin.
9 Replies

Avatar

Former Community Member
Do it the other way around .....on the change event of the radio button group:



if ((this.rawValue == "Yes") && (textField.rawValue == null)){

app.alert("TextField cannot be empty with this option")

}

Avatar

Former Community Member
Hi !



Thanks for the reply. Unfortunately, it does not work either. There seems to be a problem when the text field is empty. It is like the validation is not performed when the text field is either left empty or erased.



As a test, I initialized the radio and text field with different combinations and I also initialize another text field of the form with the value of the test. This field correctly displays either True or False, even when the first text field is empty.



Am I missing something obvious ?

Avatar

Former Community Member
Humm...



I am not completly sure to have understood you right.

Some questions for better understanding.



1) Do you want to validate the textfield WHEN you click the button (that means if it is empty a message appears when you click)

2) Do you want a user to be able to change the textfield entry but not erase it?

3) Do you want to make a validation before sending the mail? So the field simply gets obligatory but has not to be filled out the moment the user clicks.



4) Did Paul's solution work somehow? Or could there additionally be a value/naming problem? (Just to make sure)



I think the main reason you are not satisfied is, because you only put the script into the change field of the RB.



I'd add a similar script into the exit field of the Textfield like:



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

{

app.alert("TextField cannot be empty with your radiobutton choice")

}



Then you can't delte the fieldvalue anymore without replacing something.



Though I am not sure, if it's possible to send the data "wrongly" though...

Avatar

Former Community Member
Hi !



1) I want to validate upon submission of the form (via http, not email)

2) I want the user to be able to edit the text field (fill, modify, erase, etc.)

3) I want to make sure that if the radio button is set to "yes", then the form cannot be submitted unless the text field has been filled

4) unfortunately it did not work.



I use LiveCycle Designer 8.05.2073.1.374024 Cipher: 128-bit. In the Script Editor, I added the following javascript test :



(radio_field.rawValue == "1") || (radio_field.rawValue == "2" && !text_field.isNull && text_field.rawValue.length > 1)



which (normally) says the following: validation is true if radio button is "no" or if radio is "yes" and text field is not null and text field is more than one character.



The test is added for the preSubmit event.



Now, if the "yes" radio button is clicked and the text field is empty, submission proceeds. Why ?

Avatar

Former Community Member
Hi !



I just had an idea. Instead of testing for nullity and/or emptyness of the text field upon submission of the form, I could make it mandatory upon clicking the "yes" radio button (and revert upon clicking "no"). So my question is: how do I do that, make a text field mandatory via javascript ?

Avatar

Former Community Member
Acrobat/Reader will run validations automatically for you and not allow you to submit unless these validatiion conditions are met. In your case the field starts off as open and based on a condition you would make it mandatory. So we merely have to make the field mandatory (under the right conditions) and allow the submit logic inherent in the client to work. So on the change event of the checkbox you can do something like this:



if (this.rawValue == 1){

TextField1.validate.nullTest = "error"

} else {

TextField1.validate.nullTest = ""

}



Then when the user tries to submit an error message will appear indicating that they must fill out the empty field before they can submit.

Avatar

Former Community Member
Hihi thought so :)



That was why I asked. ^^

It was my first question in here too... though I have to say, I think my scripts have gotten better since that time ;D



One solution:

http://www.adobeforums.com/webx?128@@.59b7ca75



Should normally work in your case too :)

Avatar

Former Community Member
ah yeah...

Forgot to tell:

I also added in the other button (same event):



if (this.rawValue == "N")

{TextField1.validate.nullTest = "disabled";

TextField1.border.edge.color.value = "255,255,255";

Textfield1.assist.toolTip.value = "This field is optional...";

xfa.layout.relayout();

}



Just to ensure it won't stay obligatory when the user simply changes his mind about the button ;D

If you don't want the colors, just delte the color field ^^



Hope these helped :)