Regex should accept only numbers and '-'

Community Alums
Not applicable

Hi,

I wrote a small script using regex for field validation which accepts only numbers and '-'. From my testing team, I got to know it is also accepting ',', '.'. Dot and Comma Kindly help

 

 

unction onChange (control, oldValue, newValue, isLoading){
if (isLoading | | newValue == ' ') {
return;
}

var re = /[0-9-]+/;
if (!re.test(newValue)){
g_form.addInfoMessage("vip_listener_port", "VIP Listener Port only accepts numbers and "-", 'error');
g_form.clearValue("vip_listener_port);
}
}
//I HAVE WRITTEN THIS CODE MANUALLY IN INSERT/CODE SAMPLE

 

 

 

 

 

 

Screenshot 2024-07-23 173427.png

 

Regards

Suman P.

1 ACCEPTED SOLUTION

Paul Curwen
Giga Sage

Try this

^[0-9 \-]+$

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

View solution in original post

4 REPLIES 4

SK Chand Basha
Giga Sage

Hi @Community Alums 

 

^[0-9-]+$

Try with this in Background scripts test with possible Cases

function validateField(value) {

    const pattern = /^[0-9-]+$/;

    return pattern.test(value);

}

const testValues = ['123-456', '123,456', '123.456', '123456'];

testValues.forEach(function(value) {

    g s .print(value + ": " + validateField(value));

});

 

Mark it Helpful and Accept Solution!! If this helps

Paul Curwen
Giga Sage

Try this

^[0-9 \-]+$

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Brad Bowman
Kilo Patron
Kilo Patron

Since this is on a Catalog Item variable, you should be using Variable Validation Regex instead of a script.

^[0-9-]+

Works fine.  Define it via the left nav menu, then select it on the Type Specification tab.

SK Chand Basha
Giga Sage

Hi @Community Alums 

 

^[0-9-]+$