Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Issues in validating mandatory fields in my templa
Thiags
Hi,
I am working on valiating my dct using formapi
Firstly, I have two types of items in my template as mandatory and non mandatory.
which type of event handler should I use for validation - OnItem Change or OnSave?
please find below my validation script and when i try to save the dcr with proper inputs it is not saving but remains as such.
my piece of javascript is
<script language="javascript" location="template-type" src="../../validation.js"/>
<script language="javascript">
IWEventRegistry.addItemHandler("/location", "onItemChange", checklocation);
IWEventRegistry.addItemHandler("/main_loc_phone", "onItemChange", checkphone);
IWEventRegistry.addFormHandler("onSave", ValidateForm);
function ValidateForm()
{
checklocation();
checkphone();
}
function checklocation()
{
var loc = IWDatacapture.getItem("/location");
var location = loc.getValue();
if (!isLocation(location))
{
loc.setValid(false);
IWDatacapture.setHighlightMode(true);
alert("Enter valid City - State/Province format\n\nExample: Duluth - GA");
loc.setFocus();
return false;
}
loc.setValid(true);
IWDatacapture.setHighlightMode(false);
return true;
}
function checkphone()
{
var ph = IWDatacapture.getItem("/main_loc_phone");
var phone = ph.getValue();
if (!isPhone(phone))
{
ph.setValid(false);
IWDatacapture.setHighlightMode(true);
alert("Enter valid phone format \n\nExample: 727-579-1111");
ph.setFocus();
return false;
}
ph.setValid(true);
IWDatacapture.setHighlightMode(false);
return true;
}
</script>
Thanks!
Thiags
Find more posts tagged with
Comments
alanhill
Hi there, I'm just doing almost the same thing, my first foray into FormAPI validation.
The way I'm doing it is to have a file of common validation code (ValdiateTextField, ValidateSelectOptions etc). Then within the main dct script I call a function "ValidateForm" as part of the onSave event.
What I've found is that if i return a 'false' from this ValidateForm script, the normal validation that Teamsite does is skipped (meaning it doesn't validate a field twice). I also found that if I had a form item that was not validated in my custome 'ValidateForm' function, if the function returned true, the standard validation would then take place as normal.
Basically in my code I've got something like...
in datacapture.cfg...
<script language="javascript" src="commonvalidation.js" location="template-type"/>
<script language="javascript" src="customscript.js" location="template-type"/>
in customscript.js...
// Event handler to run validation on form save event
//
IWEventRegistry.addFormHandler("onSave",ValidateForm);
function ValidateForm()
{
// Validate all Items in the DCT.
//
ValidateTextItem(itemname,sRegex);
in 'commonValidation.js'...
// Standard Text Item Validation
//
function ValidateTextItem(sTextItem,sRegex)
{
var sReturn = "";
// Not empty
//
if ( sTextItem.getValue() == "")
{
some code goes here to configure message to users (populating string called sReturn)
}
// Validation Regex (if applicable)
//
if ( sRegex.test( sTextItem.getValue() ) )
{
some code here to handle validation fail and report problem to user (populating string called sReturn)
}
}
return(sReturn);
}
Hope that's of some use to you.
Cheers
Alan
Teamsite Template Developer
Thiags
Hi Allan,
Thanks buddy, it worked..
-Thiags