How to restrict to enter letters in zip code and phone number in catalog form

Bala13
Kilo Guru

Hi,

We have a catalog form which used in Service portal.

We have zip code field and phone number field.

Now, zip code and phone numbers are allowing letters in to that fields. We need to restrict to enter letters in zip and phone numbers.

Only valid zip code and phone numbers should be entering in to that.

How to achieve this?

I have tried below code but its not working, can anyone help me on this please?

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.clearMessages();
var name = g_form.getValue('former_address_zip'); // Replace your ZIP Code Field
if(name == "")
return;
var regex = /^[0-9-+()]*$/;
if(!regex.test(name)) {
g_form.showFieldMsg('former_address_zip', 'Please Enter valid number', 'error');// Replace your ZIP Code Field
g_form.clearValue('former_address_zip');// Replace your ZIP Code Field
}


}

 

1 ACCEPTED SOLUTION

Hi,

then you need to use script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	g_form.clearMessages();

	if(oldValue != newValue){
		var regex = /^[0-9-+()]*$/;
		if(!regex.test(name)) {
			g_form.clearValue('former_address_zip');// Replace your ZIP Code Field
			g_form.showFieldMsg('former_address_zip', 'Please Enter valid number', 'error');// Replace your ZIP Code Field
		}
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

13 REPLIES 13

Ankur, It should accept all zip codes.

Mainly it should restrict letters.

Hi,

then you use OOB number variable validation regex and attach to your variable

find_real_file.png

find_real_file.png

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

How to clear off the value Ankur? 

 

After the error message shows up i need to clear of the value.

Could you please help me on this?

Hi,

then you need to use script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	g_form.clearMessages();

	if(oldValue != newValue){
		var regex = /^[0-9-+()]*$/;
		if(!regex.test(name)) {
			g_form.clearValue('former_address_zip');// Replace your ZIP Code Field
			g_form.showFieldMsg('former_address_zip', 'Please Enter valid number', 'error');// Replace your ZIP Code Field
		}
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader