CMDBuild Forum

Validation rule help

I’ve made a validation rule for an attribute (of type STRING, 20, Plain Text). The field should contain a valid MAC Address or nothing, so i made this validation rule:

if(!api.testRegExp(/^$/gm,value) && !api.testRegExp(/^([a-fA-F0-9][a-fA-F0-9]:){5}([a-fA-F0-9][a-fA-F0-9])$/gm,value)) {
return ‘MAC adressen må være på formatet de:ad:b3:3f:de:ad’;
}

Validation works fine for the second part (e.g. de:ad:b3:3f:de:ad), but I cannot save with the field empty. I have to type something in the field and then delete the text for the validation rule to apply it seems. Any idea how to fix this? Can I use something other than testRegExp to check for empty fields?

I would really like some documentation on validation rules and auto value in general, especially what functions are available.

1 Like

I eventually found the solution myself. When you open the form, the value is set to null, so this code works:

if(value != null && value != '' && !api.testRegExp(/^([a-f0-9][a-f0-9]:){5}([a-f0-9][a-f0-9])$/gm,value)) {
    return 'MAC adressen må være på formatet de:ad:b3:3f:de:ad';
}
1 Like