MAC Address Validation using Client Script and Business Rule

selvambala
Tera Contributor

Hi

I have to validate MAC Address in CMDB using Client script and Business rule and format should be like this 00:0c:29:61:6d:37

how can I validate it by using RegEx.

Thanks,
Selvam Balakrishnan

6 REPLIES 6

Kartik Sethi
Tera Guru
Tera Guru

Hi @selvambala 

 

You can use the below-provided regex to validate the MAC Address:

^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$

This regex will validate formats like "00:0c:29:61:6d:37" and "00-0c-29-61-6d-37".

 

Also, let me know if you need help with script.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi Kartik,

When we provide MAC Address value if format is incorrect then MAC Address field value should be cleared – Client Script

When MAC Address value is updated from Integration system if format is incorrect then MAC Address field value should be cleared and display the Error message - Business Rule

Can you please help me?

Thanks,
Selvam Balakrishnan

 

Wrote the client script and its working.

var special_pattern = new RegExp("^([0-9-A-Z-a-z]{2}):([0-9-A-Z-a-z]{2}):([0-9-A-Z-a-z]{2}):([0-9-A-Z-a-z]{2}):([0-9-A-Z-a-z]{2}):([0-9-A-Z-a-z]{2})$");
if(!special_pattern.test(newValue))
g_form.clearValue('mac_address');

Hi @selvambala 

 

For Client Script:

Ensure â†’

UI Type is All (to run of Native, Portal, and Agent Workspace)

Type is onChange

Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
	
	var mac = g_form.getValue('<mac_address_field_name_goes_here>'),
		macPattern = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/i;
	
	if(macPattern.test(mac)) {
		//Valid MAC Address code goes here
	} else {
		//Invalid MAC Address code goes here
		g_form.clearValue('<mac_address_field_name_goes_here>');
		g_form.setMandatory('<mac_address_field_name_goes_here>', true);
	}
}


For restricting Integration you can have a Business Rule (Data Policy is also an option😞

Ensure â†’

When: Before

Insert: True

Update: True

Filter Condition:

MAC Address is not empty

Script:

(function executeRule(current, previous /*null when async*/) {
	var mac = current.getValue('<mac_address_field_name_goes_here>'),
		macPattern = /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/i;
	
	if(macPattern.test(mac)) {
		//Valid MAC Address code goes here
	} else {
		//Invalid MAC Address code goes here
		current.setValue('<mac_address_field_name_goes_here>', '');
		gs.addErrorMessage('Provide correct MAC address');
	}
})(current, previous);

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik