Jump to content

Have to have exact number in a field.


Carrie Dodt

Recommended Posts

You can certainly use JavaScript to validate that a particular string (such as the value of a data field) meets certain criteria. A "brute force" way would be something like this:

var s = Field("YourFieldName");
if (s.length != 6)
   return "invalid data; must be 6 characters";
//else
for (var i in s)
{
   if (s[i] != '0' && Int(s[i]) == 0)
       return "invalid data; must be all numbers";
}
//else
return "OK";

However, that's not very succinct code. JavaScript, especially with its very powerful Regular Expression functionality, is actually great for this doing kind of data validation, without having to write so much code, and many web pages use JavaScript validation logic for web form entries.

 

The most JavaScript-y way to check that a string consists of exactly 6 numbers (digits) to try to match it against the Regular Expression /^\d{6}$/ , which means "between the start (^) and end ($) of the string, exactly 6 digits (\d)". So you could use that in a rule like so:

if (!Field("YourFieldName").match(/^\d{6}$/))
   return "invalid data";
//else
return "OK";

 

But the tricky part, in terms of a FusionPro job, is what you mean by "return an error." If the place where the end user is entering this data is in another application, such as a web form (in MarcomCentral or a different application), then any logic to return an error message to the user at data entry time (like popping up a message box) would have to be part of that other application.

 

At composition time, in FusionPro, there are a few things you can do to either denote an error in the output, or to prevent generation of the output in the first place, if the input data is not valid. The simplest is probably something like this:

if (!Field("YourFieldName").match(/^\d{6}$/))
   return '<span color="Red">Invalid entry in field "YourFieldName"; value must be exactly six digits.</span>';
//else
return TaggedDataField("YourFieldName");

That would show the error message, in Red, in place of the field value, in the output.

 

Another thing you could do is write a message to the composition log (.msg) file, like so:

if (!Field("YourFieldName").match(/^\d{6}$/))
   Print('Error: Invalid entry in field "YourFieldName"; value must be exactly six digits.';

 

If you want to, say, have the entire composition error out if the entry is invalid, you could do this:

if (!Field("YourFieldName").match(/^\d{6}$/))
   FusionPro.Composition.Abort('Error: Invalid entry in field "YourFieldName"; value must be exactly six digits.');

 

Or, if you just want to skip that particular record of data in a VDP job, but still let other records compose and make output:

if (!Field("YourFieldName").match(/^\d{6}$/))
   FusionPro.Composition.Abort('Error: Invalid entry in field "YourFieldName"; value must be exactly six digits.', true);

(Where the "true" parameter means "skip this record only, instead of aborting the whole job.")

 

These are just a few of the ways in which you can handle the error condition, where the data field doesn't meet the criteria. But you can really do whatever you want, depending on your workflow.

 

So to recap, testing a field value to make sure it conforms to a particular pattern is pretty straightforward, and JavaScript is great for doing that, but what's not so straightforward is exactly how you want the error condition of the data not conforming to the pattern to be handled, and how that should be expressed to a user at some step in your job's workflow of data entry through composition.

Link to comment
Share on other sites

Thanks very much for your help. I did get an error message to return, however can I change that message to say: "Six digits must be input for the Cost Center."

Okay, so this gives me a bit more information, but still leaves some out.

 

What I can see from your screenshot, which was not obvious before, is that you're asking specifically about showing an error message in the context of the MarcomCentral application, where the user fills out the form and sees the template preview.

 

First of all, I believe there is a way to set up validation on those input fields on the form in MarcomCentral, so that you get a message right there on the form, before a FusionPro composition even takes place. That's a question for the MarcomCentral application forum.

 

That said, what your post leaves out is exactly which of the suggestions in my previous post you tried. I assume it's the one that calls FusionPro.Composition.Abort() to prevent any output from being generated at all.

 

If you instead want to make output, but have it show a more specific message, so that the message appears on that MarcomCentral preview, that's basically what my first suggestion was:

if (!Field("YourFieldName").match(/^\d{6}$/))
   return '<span color="Red">Invalid entry in field "YourFieldName"; value must be exactly six digits.</span>';
//else
return TaggedDataField("YourFieldName");

Though if you want something more "in your face," you could just make a giant text frame that covers the whole page, with that message in it, and have a rule slightly different than the one above to show a message if the validation fails, or nothing at all if it succeeds:

if (!Field("Cost Center").match(/^\d{6}$/))
   return 'Six digits must be input for the Cost Center.';
//else
return '';

Then put that rule into the giant text frame, with no frame fill, at the top layer, in 200 point red text or something. When the input is okay, you'll see nothing in that frame, but if it's not okay, the error message will appear.

 

Another option is to make a frame with the error message text typed into it, with a frame fill color (probably white), name that frame, then in OnRecordStart, selectively show or hide the frame:

FindTextFrame("Error Message Frame").suppress = !Field("Cost Center").match(/^\d{6}$/));

Again, when the input is okay, that frame will be suppressed, but if it's not, it will show up and cover the other content on the page.

 

There are a lot of other similar things you could do. But, like I said at at the start, I'm pretty sure there's a way to do the validation in MarcomCentral, and not have to have it fall to FusionPro at all.

Link to comment
Share on other sites

So again, you should ask about this in the MarcomCentral app sub-forum, or Service Desk, or ask your CPM/BRM/support person, because I'm not an expert in how to set up a template there. But, in the admin tool, under Template Setup, under the Form Display tab, you can click on the field name to edit the a data field, and then under "Configure Custom Form Validation Rule," you can set up the validation code for that field. The idea here is that the data entry validation happens on the web form, before the job even tries to preview in FusionPro.

 

http://forums.pti.com/attachment.php?attachmentid=2011&stc=1&d=1570062065

 

Once again, though, for exactly what you need to type in there, you should ask on the MarcomCentral forum, or Service Desk, or contact your CPM or MarcomCentral Support.

MCC-field-config.jpg.b6198a34ea5811f0a20969313162c5c0.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...