Expand my Community achievements bar.

textfield validation

Avatar

Level 5

Hi All.

I would like to validate TextField1 for two conditions: if fied is empty and cannot be exceed 7 charecters. How do by code?

Thanks.

6 Replies

Avatar

Level 5

I tried to validate using that code

if (this.length = 0 or MedRec.text.length < 7)
{
   MedRec.border.edge.color.value = "255,255,255";
   MedRec.assist.toolTip.value = "Please, check value of Field.";
}

Avatar

Former Community Member

You have javascript errors ..... a comparison in javascripot requires two equal signs ...one equal sign means that you are assigning the value.  When using the "this" keyword that is an object....so you need to get the rawvalue of the field to determine its length. The or operator is a || not thw word or. Not sure what you are trying to test in the 2nd part of the expression. See the modified code below:

if ((this.rawValue.length == 0) || (MedRec.text.length < 7))
{
   MedRec.border.edge.color.value = "255,255,255";
   MedRec.assist.toolTip.value = "Please, check value of Field.";
}

Paul

Avatar

Level 5

Hi Paul,

I woulld like to validate MedRec TextField that must to have only 7 characters on EXIT event. If field is empty or more or less than 7 characters in that case user should get tooltip or error message and border of that field changed to RED color. If user entered 7 characters, just move to other form field. I cannor figure out why the code

if ((MedRec.tex.length == 0) || (MedRec.text.length < 7)) || (MedRec.text.length < 7))
{
   MedRec.border.edge.color.value = "255,255,255";
   MedRec.assist.toolTip.value = "Please, check value of Field.";
}

doesn't give error message if I leave that field empty or type wrong amout of characters?

Thanks.

Avatar

Former Community Member

Set  the max chars on the Object palette to 7 chars  and then there is no need to check for more than 7 chars as you have limited the field to have 7 chars only. Then you only need to check for less than 7 .....This is the code that you want:

if (MedRec.rawValue.length < 7) {

     do your stuff here

}

Paul

Avatar

Level 5

Hi Paul,

My code now


if (MedRec.rawValue == null) || (MedRec.rawValue.length < 7))
{
MedRec.fontColor.value = "255,255,255";
xfa.host.messageBox("The field Med Rec# doesn't have 7 characters.");
}

In case message is pop up but font color didn't changed. And other problem. If I try use code

if (MedRec.rawValue.length < 7))
{
if (MedRec.rawValue == null)
{
xfa.host.messageBox("The field Med Rec# cannot be empty.");
}
MedRec.fontColor.value = "255,255,255";
xfa.host.messageBox("The field Med Rec# must have 7 characters.");
}

Message didn't pop up if I leav field empty

Avatar

Former Community Member

Is your form saved as dynamic? That may be what is stopping the font color from changing.

If you are using Acrobat to preview the form then you can hit Ctrl-J to bring up the java console and any errors in your code will be reported there. This shoudl help you debug the issue with the 2nd part. You will see that you have an extra ) at the end of the 1st expression. Also you have a logic error. You cannot get a length of MedRec if it is null as it has no value so it is never getting into your code. Do the test for null first, then do the test for the length < 7.

Paul