How to check if a field has a certain string mentioned in it.

RohanDS
Tera Contributor

Hello all,

 

I have a requirement where I need to check the below two conditions. If the conditions are NOT met, the user will not be able to submit the request and an alert will be shown to him. The conditions are as below:

1. The field Tool should have a selection Box

2. The field URL should contain the string ".box." (in any case)

 

Basically, when someone selects Box the URL they provide has to have .box. string in it.

 

I have tried below onSubmit script but it isn't working for me.

************************************************************************************************************

function onSubmit() {
    if (g_form.getValue('select_the_tool') == 'box' && g_form.getValue('url_of_file_sharing_site_zscaler').indexOf('.box.') != 1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}
2 ACCEPTED SOLUTIONS

Slava Savitsky
Giga Sage

The function indexOf() returns -1 if the substring is not found, so you should check for that value. Try this:

function onSubmit() {
    if (g_form.getValue('select_the_tool') == 'box' && g_form.getValue('url_of_file_sharing_site_zscaler').indexOf('.box.') == -1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}

 

View solution in original post

Sandeep Rajput
Tera Patron
Tera Patron

@RohanDS Please try the following script.

 

function onSubmit() {
    if (g_form.getValue('select_the_tool') == 'box' && g_form.getValue('url_of_file_sharing_site_zscaler').indexOf('.box.') == -1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}

Hope this helps.

View solution in original post

4 REPLIES 4

Slava Savitsky
Giga Sage

The function indexOf() returns -1 if the substring is not found, so you should check for that value. Try this:

function onSubmit() {
    if (g_form.getValue('select_the_tool') == 'box' && g_form.getValue('url_of_file_sharing_site_zscaler').indexOf('.box.') == -1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}

 

Thanks! this works.

Sandeep Rajput
Tera Patron
Tera Patron

@RohanDS Please try the following script.

 

function onSubmit() {
    if (g_form.getValue('select_the_tool') == 'box' && g_form.getValue('url_of_file_sharing_site_zscaler').indexOf('.box.') == -1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}

Hope this helps.

Brad Bowman
Kilo Patron
Kilo Patron

You are alerting on the first condition you described being met.  It also sounds like in this case, you want to use OR, not AND since someone could pick the tool = box but not the URL, and vice-versa.  If the url is the type of URL you may need to force it to a string before using the indexOf function - can't hurt in any event, and this also accounts for 'box' in any case:

 

function onSubmit() {
    if (g_form.getValue('select_the_tool') != 'box' || g_form.getValue('url_of_file_sharing_site_zscaler').toString().toLowerCase().indexOf('.box.') == -1) {
        alert('Make sure the URL to unblock is related to Box or select the tool as "Other" to proceed.');
        return false;
     }
}

 

If it's still not working the way you are expecting, verify the field/variable names and values are exactly correct and alert on the two getValues to find out why each condition is or is not being met with your test case.