Regex - Starts with specific text and ends with number on Change

piyushnector
Tera Contributor

Hello,

 

I am looking for a solution on below - 

 

There is a field in the catalog item in which the user should only allow to enter specific text (STARTS with "ADA") and ends with a number. No special symbol is allowed.

 

This should be an immediate response, if the pattern does not match we should be getting an error (popup) message.

 

Example Text: ADARegionCountry5004

 

Regards,

Piyush Kumar

1 ACCEPTED SOLUTION

Hi @piyushnector ,

 

To make the regular expression case-insensitive, you can add the "i" flag at the end of the regular expression pattern. This will allow it to match both uppercase and lowercase letters.

 

Update this line var pattern = /^ADA.*\d$/;  to  var pattern = /^ADA.*\d$/i;

 

Regards,

Shravan.

Please mark as helpful and correct solution

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

View solution in original post

8 REPLIES 8

Sai Shravan
Mega Sage

Hi @piyushnector ,

You can use a client-side catalog client script to validate the text entered in the field against your pattern and display an error message if it does not match. Here's an example script that you can modify to fit your needs:

function onSubmit() {
    var field = g_form.getValue('u_text_field');

    // Define your pattern using a regular expression
    var pattern = /^ADA.*\d$/;

    // Check if the entered text matches the pattern
    if (!pattern.test(field)) {
        // If the pattern does not match, display an error message
        alert('The text must start with "ADA" and end with a number.');
        return false;
    }

    return true;
}

In this script, g_form.getValue('u_text_field') retrieves the value of the catalog item text field. The regular expression ^ADA.*\d$ matches any string that starts with "ADA" and ends with a number.

 

The test() method of the regular expression object is used to check if the entered text matches the pattern. If it does not match, an alert message is displayed and the form submission is prevented by returning false.

 

You can add this script to your catalog item in the "Client Script" section and select the "onSubmit" event. This will trigger the validation when the user submits the catalog item form.

 

Regards,

Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Thanks for the response but I need to validate as soon as user enters and leaves the field, onChange 

Hi @piyushnector ,

 

Yes, you can modify the script to trigger on the onChange event instead of the onSubmit event by changing the function name and adding an event parameter. Here's an example:

 

function validateTextField() {
    var field = g_form.getValue('u_text_field');

    // Define your pattern using a regular expression
    var pattern = /^ADA.*\d$/;

    // Check if the entered text matches the pattern
    if (!pattern.test(field)) {
        // If the pattern does not match, display an error message
        g_form.showFieldMsg('u_text_field', 'The text must start with "ADA" and end with a number.', 'error');
        g_form.clearValue('u_text_field');
    }
}

In this script, g_form.showFieldMsg() displays an error message below the text field and g_form.clearValue() clears the field value if it does not match the pattern.

 

You can add this script to your catalog item in the "Client Script" section and select the "onChange" event for the u_text_field field. This will trigger the validation when the user changes the value of the text field.

 

Regards,
Shravan

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

It is not working, every time it is giving error doesn't matter any value I enter.