- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 01:19 AM
Hello All,
I have one variable (Type - single line text) and I need a Regex Pattern that allows users to enter a Subnet address in the format of xxx.xxx.xxx.xxx/xx
Solution
- Create a Catalog item.
- Edit/Create a new variable on which you want to add the validation.
- Configure an on-change client script using the below regex to the user input.
//This is a regex to validate Subnet address format of xxx.xxx.xxx.xxx/xx
^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))$
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Client script to validate subnet address format of xxx.xxx.xxx.xxx/xx
if (isLoading || newValue == '') {
return;
}
var regExp = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))$/;
var isValid1 = regExp.test(newValue);
if (!isValid1) {
g_form.showErrorBox("enter_vlan_address", "Subnet must match the format of xxx.xxx.xxx.xxx/xx", true);
}
}
***
If you face any problem or need any kind of assistance please feel free to message me.
Thanks for visiting my article. If the article helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.
See you soon,
Prasad.
Solved! Go to Solution.
- 1,054 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 03:24 AM
Have a nice day @Matt102 ,
1. If you don't want to accept the leading and trailing whitespaces, please use the below regex:
^[^\s]((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))+[^\s]$
2. Your second doubt can be resolved in 2 ways:
a. Preventing client-side form submission is very simple. All you need to do is set up an ‘onSubmit’ client script and return ‘false’ in the script along with regex matching logic. i.e. we need to create 2 client scripts: onChange and onSubmit.
b. Clear the VLAN Address single line text field if the user gives invalid input and make that field mandatory.
We can use g_form.clearValue(); function to clear the field value.
So 2nd approach is more feasible.
Thanks,
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 02:06 AM
Hi,
I was wondering, would you want to account for leading/trailing whitespace?
Also, (not sure about this bit), what would the effect be if this var was the last filled in field and immediately afterwards the submit button was clicked, would the onChange kick in?
r,matt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2022 03:24 AM
Have a nice day @Matt102 ,
1. If you don't want to accept the leading and trailing whitespaces, please use the below regex:
^[^\s]((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\/([0-9]|[1-2][0-9]|3[0-2]))+[^\s]$
2. Your second doubt can be resolved in 2 ways:
a. Preventing client-side form submission is very simple. All you need to do is set up an ‘onSubmit’ client script and return ‘false’ in the script along with regex matching logic. i.e. we need to create 2 client scripts: onChange and onSubmit.
b. Clear the VLAN Address single line text field if the user gives invalid input and make that field mandatory.
We can use g_form.clearValue(); function to clear the field value.
So 2nd approach is more feasible.
Thanks,
Prasad