Single Line text variable validation in catalog form

Community Alums
Not applicable

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. 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

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

View solution in original post

2 REPLIES 2

Matt102
Giga Guru

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

Community Alums
Not applicable

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