Expand my Community achievements bar.

SOLVED

Validate Field based on String length

Avatar

Level 2

Hi all,

 

I want to validate a text field (string, named "Kontierungselement") based on the length. So allowed are 10, 8 or 7 char. I limited the field to 10 char. But for every other input there should be an error message. I put the error message in the validate script message of the object and tried it with this code inside the validate event. 

But my problem is that the length (currentLength) is not working - I all the time see 0 instead of the correct value, any ideas?

 

FormServiceRequestConfirmation.mstPageSet1.mstPage1.frmTitle.Kontierungselement::validate - (JavaScript, client)
console.show();
var currentLength = xfa.event.newText.length;
console.println(currentLength);
var kontierungselement = FormServiceRequestConfirmation.mstPageSet1.mstPage1.frmTitle.Kontierungselement;
console.println(kontierungselement.length);

if(currentLength != 10 && currentLength != 8 && currentLength != 7){
result = false;
}

 

Thanks a lot! 

Deborah

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi there,

 

if you are using xfa.event.newText, it should be within the "enter" event. 

Any other event will need to use this.rawValue instead.

 

I hope this will help

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

Hi there,

 

if you are using xfa.event.newText, it should be within the "enter" event. 

Any other event will need to use this.rawValue instead.

 

I hope this will help

Avatar

Level 2

Hi @Magnus069,

if I use this.rawValue.length and still the validate event I got the following error:

 

TypeError: this.rawValue is null
5:XFA:FormServiceRequestConfirmation[0]:mstPageSet1[0]:mstPage1[0]:frmTitle[0]:Kontierungselement[0]:validate

 

What can I do? 

Avatar

Level 10
When using rawValue , it will need you to verify first that the rawValue is not null. So, technically you need to verify with a if statement first that the rawValue isnt null, if it is null, then you can consider the validation to return false. Otherwise, if the rawValue is different than null, that is when you can handle the data and verify its length.

Avatar

Level 10

The rawValue is only updated when you exit a field.

You can use a script with regular expression in the exit event to test against the rawValue.

 

if (this.rawValue.match(/^(\D{7,8}|\D{10})$/) === null) {

    xfa.host.messageBox("Invalid value entered.");
}