Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

Validate Field based on String length

deboraha5380687
Level 2
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
Magus069
Correct answer by
Level 10
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
Magus069
Correct answer by
Level 10
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

deboraha5380687
Level 2
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? 

Magus069
Level 10
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.
radzmar
Level 10
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.");
}