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

Danish Bhairag2
Tera Sage

Hi @J_31 ,

 

If you want the script to run on change of both the fields i.e business application n description then u do need to create on change script for both. Current script u can just copy paste n use it for business application field as well.

 

Also if the form will be used on portal please keep UI type as ALL. Currently it's set to desktop on portal that validation will not work.

 

Thanks,

Danish

 

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