I need to be able to validate a string entered is in the correct format e.g. for National Insurance Number two characters\six digits\1 character i.e.aa999999a.
This calc would perform the required validation using regex comparison on the string. Altering the regex would allow for validation of other strings.
import System
static def GetAttributeValue(Request):
if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:
Value = "NI valid"
else:
Value = "NI invalid"
return Value
I am having difficulty getting the calc to run and also display back to the window that it is invalid?
I had hoped to apply a Before Save calc directly to the NI Number attribute - a string with length 20 - where the intention would be add to the entered data to inform the user it was not in the correct format by using this calc with a dependency set as _NINumber
import System
static def GetAttributeValue(Request):
if Request._NINumber != null:
if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:
Value = Request._NINumber
else:
Value = Request._NINumber + " invalid format"
return Value
When running test calculation within the edit formula window it returns expected values, Unfortunately when you OK and then attempt to Save the attribute with the calc it fails with message
Error on saving changes:
Cannot calculate an attribute if that attribute is used in a calculation.
I then tried to create a separate windows calc Boolean field with a dependency set as _NINumber which I would then use as a trigger to write the validation message back to a third output attribute.
import System
static def GetAttributeValue(Request):
value = false
if Request._NINumber != null:
if Request._NINumber =~ /^[a-zA-Z]{2}\d{6}[a-zA-Z]$/:
Value = true
return Value
Again the test calculate returned expected results. However because the original trigger - NI Number - is a string attribute I can't get the calculations to fire.
The only thing I can come up with is to have a separate Boolean which the user would have to turn on to fire the validation against fields already entered and construct a string advising about all the fields which may be wrong. Would prefer to do this automatically so that it doesn't get fired before the fields have been entered. I've also got to figure out a way to reset it so it can be done again after any attempted corrections.
Anyone got any ideas on how best to achieve this or have a different approach to try.