As you know the EDIValidator component uses rules files to validate EDI data.  You can specify rather complex conditions.  For example

+SegPos[322] = if ((SegPos[316:1] == "R") or (SegPos[316:1] == "S")) then Usage[Required] else Usage[Optional] end

This rules says that segment number 322’s usage depends on whether the condition in the if statement is true.  The following example also contains else if clauses and creates an error if any condition is met

+SegPos[275:2]=if ( SegPos[275:2] !=  SegPos[3:2] ) then Error[ElementHasWrongValue,"ST02 must be equal to SE02"] end elseif ( SegPos[275:2] !=  SegPos[3:3] ) then Error[ElementHasWrongValue,"ST02 must be equal to SE03"] end else Error[ElementHasWrongValue,"ST02 must be equal to SE05"] end

But what if you need more powerful conditions that the rules file language doesn’t support?  Let me introduce Code Conditions


Code Conditions allow you harness the power of .Net to validate data.  When the EDIValidator encounters a code condition it throws an event.  You guys still with me?  OK, I’ll show you how it’s done.

Scenario 1

Your Boss: “Steve, we need to make sure that all of our Interchange Control Numbers in segment IEA does not start with ‘0000‘ or YOU’RE FIRED!”

You: “No prob boss dude, I’ll use a code condition”

You Boss: “This better work Steve!”


OK, so we need to target element 2 of the IEA segment


Step 1: Update our rules file

We’ll add the following rule

+SegPos[277:2]=if ( CodeCondition["shouldStartWithZeros"] ) then Error[ElementHasWrongValue,"Yeah, I have a job"]  end

We can see from the image above that segment 277 is the IEA segment.  What we’ve done is add a code condition that will call our .Net code.  We named this code condition ‘shouldStartWithZeros‘ so that we can identify it in our code.

Step 2: Register for the OnCodeCondition event

Register for the appropriate event on the EDIValidator

ediValidator.OnCodeCondition += new EDIValidator.CodeConditionEvent(ediValidator_CustomCondition);

Step 3: Write our own condition check

private void ediValidator_CustomCondition(object sender, CodeConditionEventArgs e){
  if (e.CodeCondition.Id == "shouldStartWithZeros"){
     if (e.CurrentSegment.Elements[1].DataValue.StartsWith("0000")){
      e.ConditionValid = true;

When you run the validator you now see the error


Scenario 2

Your Boss: “Steve, you are not fired yet.  I also need to check if the ISA segment element 1 is equal to ’00’ in that condition.  Don’t ask why”

You: “No prob boss dude, I can use the same code condition”

You Boss: “This better work!”

Step 1: Just use the FoundSegments collections

private void ediValidator_CustomCondition(object sender, CodeConditionEventArgs e){
  if (e.CodeCondition.Id == "shouldStartWithZeros"){
     if (e.CurrentSegment.Elements[1].DataValue.StartsWith("0000") && e.FoundSegments[0].Elements[0].DataValue=="00"){
      e.ConditionValid = true;

You: “Boss dude, there are a few properties I can use to take care of all the scenarios”

Your Boss: “You’re fired! WAIT, what did you say?”

You: “I can use the CurrentSegmentSchema to check what our current segment structure should look like. I can use the DataLoop property to see all the loops and segments that were loaded so far.  This can enable us to validate against any data we want!

Your Boss:”Wow, you can go home early today.  Just kidding.  But you’re not fired”

Take Charge Of EDI

RDPCrystal EDI Library