Validate on form portal

Dipu_9999
Tera Expert

"When a specific field contains only an integer value, the form should be submitted. However, if any changes are made to the field's value using the browser's inspect tool, or if a string value is entered, the form should not be submitted, and an error message should be displayed. To achieve this, please use the following client-side script on submit:"

DipaliLidhure1_0-1695264749241.png

6 ACCEPTED SOLUTIONS

AnveshKumar M
Tera Sage
Tera Sage

Hi @Dipu_9999 

You can set the variable REGEX in type specification to "Number" for OOTB validation or you can can use the onSubmit client script I provided below.

AnveshKumarM_0-1695261474236.png

 

 

Or Use the following code to allow only integers (replace with your variable names)

 

 

 

 

function onSubmit() {
    var intVar = g_form.getValue('please_enter_value') + ''; //Replace with your form variable name
	var regEx = /^[0-9]+$/;
    if (regEx.test(intVar) == false) {
		g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
        return false;
    }
}

 

 

 

Or Use the following code to allow real numbers (replace with your variable names)

 

 

function onSubmit() {
    var intVar = g_form.getValue('please_enter_value') + ''; //Replace with your form variable name
	var regEx = /^(?:-(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))|(?:0|(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))))(?:.\d+|)$/;
    if (regEx.test(intVar) == false) {
		g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
        return false;
    }
}

 

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

 

Thanks,
Anvesh

View solution in original post

Hi @Dipu_9999 ,

 

It seems you are using Multi Row Variable Set. Try using the below code in onSubmit catalog client script at catalog item level (not at variable set level).

 

function onSubmit() {
    var mrvs_data = JSON.parse(g_form.getValue('multi_row_variable_set_internal_name')); //Replace with your multi row variable set internal anme
    var regEx = /^[0-9]+$/;
    for (key in mrvs_data) {
        if (regEx.test(mrvs_data[key].SIZE_IN_GB) == false) { //Replace with the varibale name of Size (in GB)
            g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
            return false;
        }
    }
	return true;
}

 

Please let me know if you still have issues.

 

Thanks,
Anvesh

View solution in original post

@Dipu_9999 

You are writing this client script in Variables set, the script I provided should be used at catalog level. In Your client script form, change the Applies To field to "A Catalog item" and a new field field will appear in place of Variable Set field that is Catalog Item, select your catalog item in which you are using this MRVS. See the screen shots below.

 

AnveshKumarM_0-1695287592077.png

 

 

If you want to write your client script at Variable Set level only, let me know.

 

 

Thanks,
Anvesh

View solution in original post

@Dipu_9999  Unfortunately onSubmit will not work at variable set level. And I have tested this script in my PDI it is working fine. Could you please delete this and create a new onSubmit catalog client script at catalog level. And use the below script to get the alerts for debugging.

 

function onSubmit() {
    var mrvs_data = JSON.parse(g_form.getValue('size_drive_partition_detail')); //Replace with your multi row variable set internal anme
    var regEx = /^[0-9]+$/;
    for (key in mrvs_data) {
        alert("Iteration : " + key + " Value: " + mrvs_data[key].size);
		alert(mrvs_data[key].size);
        if (regEx.test(mrvs_data[key].size) == false) {
            g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
            return false;
        }
    }
	return true;
}

 

If you receive the alerts and still the validation is not working we have to check the variable names and all are configured correctly or not.

 

Please let me know if still the issue persists.

Thanks,
Anvesh

View solution in original post

@Dipu_9999 

Please see the results from my PDI

Invalid Integer Case:

AnveshKumarM_0-1695297400642.png

 

Valid Integer Case:

AnveshKumarM_1-1695297452217.png

 

Empty value Case:

AnveshKumarM_2-1695297537047.png

 

Thanks,
Anvesh

View solution in original post

@Dipu_9999  try this script

function onSubmit() {
    var mrvs_data = JSON.parse(g_form.getValue('size_drive_partition_detail')); //Replace with your multi row variable set internal anme
    var regEx = /^[0-9]+$/;
    for (key in mrvs_data) {
        if (mrvs_data[key].size == '') {
            g_form.addErrorMessage("Please fill the size(in GB) and submit.");
            return false;
        } else if (regEx.test(mrvs_data[key].size) == false) {
            g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
            return false;
        }
    }
	return true;
}
Thanks,
Anvesh

View solution in original post

12 REPLIES 12

AnveshKumar M
Tera Sage
Tera Sage

Hi @Dipu_9999 

You can set the variable REGEX in type specification to "Number" for OOTB validation or you can can use the onSubmit client script I provided below.

AnveshKumarM_0-1695261474236.png

 

 

Or Use the following code to allow only integers (replace with your variable names)

 

 

 

 

function onSubmit() {
    var intVar = g_form.getValue('please_enter_value') + ''; //Replace with your form variable name
	var regEx = /^[0-9]+$/;
    if (regEx.test(intVar) == false) {
		g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
        return false;
    }
}

 

 

 

Or Use the following code to allow real numbers (replace with your variable names)

 

 

function onSubmit() {
    var intVar = g_form.getValue('please_enter_value') + ''; //Replace with your form variable name
	var regEx = /^(?:-(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))|(?:0|(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))))(?:.\d+|)$/;
    if (regEx.test(intVar) == false) {
		g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
        return false;
    }
}

 

 

Please mark my answer helpful and accept as solution if it helped 👍✔️

 

Thanks,
Anvesh

Dipu_9999
Tera Expert

I tried all these way but its not working when i fill the string value from doing Inspect and form is submitted so i wants to avoid these. only integer can be accepted form should not be submitted.

DipaliLidhure1_0-1695264749241.png

 

Hi @Dipu_9999 ,

 

It seems you are using Multi Row Variable Set. Try using the below code in onSubmit catalog client script at catalog item level (not at variable set level).

 

function onSubmit() {
    var mrvs_data = JSON.parse(g_form.getValue('multi_row_variable_set_internal_name')); //Replace with your multi row variable set internal anme
    var regEx = /^[0-9]+$/;
    for (key in mrvs_data) {
        if (regEx.test(mrvs_data[key].SIZE_IN_GB) == false) { //Replace with the varibale name of Size (in GB)
            g_form.addErrorMessage("Please input a valid integer and resubmit the request.");
            return false;
        }
    }
	return true;
}

 

Please let me know if you still have issues.

 

Thanks,
Anvesh

Yes Correct, I am using multi row variable set and i tried this script but its not working 

DipaliLidhure1_0-1695286712284.png