Client script to validate text

J_31
Kilo Sage

Hi I want to validate the text entered inside the first two question using client script 

below is the script - should I want display error message below the field ; 

script is working for description but not for the first one, 

also should I try the same using on submit? 

image.jpg

image.jpg

1 ACCEPTED SOLUTION

Aniket Chavan
Tera Sage
Tera Sage

Hello @J_31 ,

Please let me know your views on my earlier queries and till then you can try with the below onSubmit client script.

onSubmit Client Script :

function onSubmit() {
    var descfield = g_form.getValue('description');
    var ba = g_form.getValue('business_application');
    const regex = /^[A-Z0-9a-z]+$/;

    var descValid = regex.test(descfield);
    var baValid = regex.test(ba);

    if (!descValid) {
        g_form.clearValue('description');
        g_form.showFieldMsg('description', 'Description must contain only alphanumeric characters', 'error');
    }

    if (!baValid) {
        g_form.clearValue('business_application');
        g_form.showFieldMsg('business_application', 'Business Application must contain only alphanumeric characters', 'error');
    }

    // Allow form submission only if both fields are valid
    if (!descValid || !baValid) {
        return false; // Prevent form submission
    }

    return true; // Allow form submission
}

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

 

View solution in original post

6 REPLIES 6

Dibyaratnam
Tera Sage

It's your choice where you want to display the error message. As it's an onchange client script for description field, it'll work only for the description field and if you make any change to the business application field, the logic will not execute.

In your case you will only be able to validate both fields iff you make changes to the description field at the end. Otherwise you can have another onchange client script or onsubmit one.

Aniket Chavan
Tera Sage
Tera Sage

Hello @J_31 ,

 

Can you please elaborate the requirement and let me know exactly what you want to validate and you can show the display error message for the field using show field message and in your case you will need either 2 on change client scripts or one onSubmit client script.  But it's better to have only one with onSubmit.

Also paste the code here if possible.

How to

acheive using single

onsubmit 

Hello @J_31 ,

Yes , so please give a try to the script which I gave you earlier and let me know your views on this.