Validation Regex for Numbers and as well allows decimal values ?

Aman_t
Tera Contributor

Hi, 

I have a requirement to create a new regex validation for catalog variable or modify the OOTB "Number" to allow the decimal value also.
Need assistance to create a regex validation for number as well as decimal values

Aman_t_0-1737702020301.png
Thanks in Advance

 

4 REPLIES 4

Simon Hendery
Mega Patron
Mega Patron

Hi @Aman_t 

 

Here's the best advice you'll ever get regarding Regex: ask ChatGPT to solve it for you!

 

Regex is one of those things you typically don't use often, so even the best devs aften forget the details in between the times they need it. On the other hand, it's the type of challenge that's quick and easy to solve by asking ChatGPT.

OlaN
Giga Sage
Giga Sage

Hi,

Although Simon is correct in getting help from ChatGPT, there are also people on the Community that likes regex questions. I'm one of them.. 😀

 

So... this validation should work for you, and also providing an example on how you can test if the validation works with your requirements.

var regexValidation = /^\d+([.]\d+)?$/;  // first match one or more digits, then an optional dot as decimal separator, and then another set of one or more digits to match

var testString = '1234.3456';

if (testString.match(regexValidation)){
	gs.info('Valid');
}
else{
	gs.info('Invalid');
}

 

 

Aman_t
Tera Contributor

Hi @OlaN , Its working but in onload client script but when I try to create a new regex validation

Aman_t_0-1738231361889.png

 

Hi,

Just skip the starting and ending "/".
These are needed when running a regex in a server side script, not when defining a catalog variable validation.