Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Regex to accept numbers and can only accept one dot for decimals.

pardhiv
Tera Contributor

I am trying to write a regex with on change client and tried the below. It needs to accept numbers as below

 

ex: (should accept numbers and dots)

 

12345

12345.67

12345.678xxx (No limitation on decimals unless its not possible)

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   //Accept numbers with only dots
   var pattern =  /^(([0-9.]?)*)+$/;  
    if (!pattern.test(newValue)) {
		g_form.clearValue('u_amount');
        g_form.showFieldMsg('u_amount', 'Accepted format : 354621.32', 'error', true);
        
    } 
   
}

 

 

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@pardhiv If you wish to have just one dot in the number and have numbers upto two decimal places then you can try the following regular expression.

/^(([0-9]+\.?[0-9]{2}?)*)+$/gm

It accepts following combinations.

1234

1234.12

 

Invalid combination

1234.123

1234.12.1

Gangadhar Ravi
Giga Sage

try this 

^\d+(\.\d{1,2})?$

 

Gangadhar Ravi
Giga Sage

try this 

 

^\d+(\.\d{1,2})?$