Livecycle Validation - Getting a Textfield substring


Have a problem with validation using Javascript in Adobe LiveCycle Designer.

I have a textfield that I want to accept up to 12 characters, or 13 characters if preceded by a + or - symbol. I can set a maxChars on the field, but that only works for 12 or 13. I was thinking of setting the maxChars attribute with the onchange event, so if the first text char is a + or - then set maxChars to 13, otherwise set to 12. Something like

if (this.rawValue.substring(0,1) != "+" && this.rawValue.substring(0,1) != "-") {
//app.alert ("starting with + or -!");
Somehow set maxChars to 12
} else {
//app.alert ("NOT starting with + or -!");
Somehow set maxChars to 13

this.rawValue.substring doesn't work, despite being valid JS afaik

this.rawValue alone doesn't have the currently typed in value, only the value that was in the TextField after the last time you exited it

It might be that we have to use FormCalc etc, but I don't really want to go that route if possible.

Who is Participating?
LordOfPortsConnect With a Mentor Commented:
The onchange event is too late, i.e. it occurrs after the user leaves the text field, the onkeyup event is needed.

To get the first character in the field try:


To set the maximum length try:

this.maxLength = 12;

I am not certain if it is same as Adobe LiveCycle however the following Javascript example is tested successfully in IE and FF:
<script type="text/javascript">
function SetMaxChars(field)
	var chFirst = field.value.charAt(0);
	if(chFirst != "+" && chFirst != "-") {
		field.maxLength = 12;
	else {
		field.maxLength = 13;
<input type="text" onkeyup="SetMaxChars(this);" maxlength="12" />

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.