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.

How to allow number range in regex

harshadakashid
Tera Contributor

There is a field call Destination port which is a single line text field and a OnChange client script is written to allow number or number range (i.e 1119 or 1100-2000)

How to write regex for this??

var regEx = /^\d+$/;
        if (!regEx.test(destination_port)) {

            g_form.showFieldMsg("destination_port", "Please enter valid Destination Port number.", "error");
         
This is only allowing number i.e 110 but it should allow number range i.e 2000-3000.
5 REPLIES 5

@harshadakashid ,

 

Can you please try this regex

 

var num_ranger = /^(\d+(-\d+)?|\d+)$/;

        if (!num_ranger.test(destination_port)) {

            g_form.showFieldMsg("destination_port", "Please enter valid Destination Port number.", "error");

}

 

 

Please mark helpful, if this helped.

 

Thanks,