To change the format of single line text type Variable

rishabh31
Mega Sage

Dear Team,

 

I have a variable named 'customer_id' of type 'Single Line Text' on a catalog item. While filling out the form I want to make this field to take ONLY the below type format, the rest of any other format is not allowed and throws the error 'Please enter Valid Format'-

Format Required: NNNNN-NNN-NN-AAA (N denotes Numeric and A denotes Alphanumeric entries; In between '-' required, as shown in mentioned format).

I believe this requires OnCange Catalog CS on variable 'customer_id', but I am not able to get this fulfilled as '-' in between values and numeric and alphanumeric characters get me into trouble.

Requesting Help to get this resolved.

 

Thanks

 

1 ACCEPTED SOLUTION

Manmohan K
Tera Sage

Hi @rishabh31 

 

Use below on change client script on the text field. do replace the field name in script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var re = /^\d{5}-\d{3}-\d{2}-[a-zA-Z0-9]{3}$;
	if (!re.test(newValue)) {
        g_form.showFieldMsg('your_field name', 'Please enter valid format', 'error');
        return false;
    }
    return true;
}

 

 

View solution in original post

3 REPLIES 3

Manmohan K
Tera Sage

Hi @rishabh31 

 

Use below on change client script on the text field. do replace the field name in script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var re = /^\d{5}-\d{3}-\d{2}-[a-zA-Z0-9]{3}$;
	if (!re.test(newValue)) {
        g_form.showFieldMsg('your_field name', 'Please enter valid format', 'error');
        return false;
    }
    return true;
}

 

 

Hi @Manmohan K Thank you, tested and works well, just a small change in regex

 var re = /^\d{5}-\d{3}-\d{2}-[a-zA-Z0-9]{3}$/;

Just applied '/' at last. Marking your solution correct and helpful.

Thanks

@Manmohan K , there is an issue, the format required is: NNNNN-NNN-NN-AAA (N denotes Numeric and A denotes Alphanumeric entries; In between '-' required, as shown in mentioned format).

 

But the AAA part should be taking alphanumeric characters but it's taking all 3 alphabets or 3 Numerics, or 3 alphanumeric. Could you please change this only to take alphanumeric not only alphabets or only numerics.

 

Kindly help