How to apply regex for getting only numeric values?

Supriya P
Tera Contributor

Hi,

I have a requirement, on "Change Request" there is field Effort Estimation "Type : String". In that field only numeric value should be filled eg: 23.55, 345, 56. For that I have written a onChange client script. Values like ASdsf233 and asd.345 are cleared But it is taking value "123Asfdg" ,"456.fgj" How to clear values like 65768Asdgf.

Client script:

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

var regexp = /^[0-9].*$/;


if (!regexp.test(newValue))
{
alert('It should be numeric fractional entry only');


g_form.setValue('u_effort', '');
}
}

1 ACCEPTED SOLUTION

Another option is to trim the spaces and test the trimmed value.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var regexp = /^[0-9]*(.)?[0-9]*$/; // may or may not have fractional. Fractional should also be numeric
	var str = newValue.trim(); // trim leading and ending spaces
    if (!regexp.test(str)) {
		g_form.clearValue('u_effort');
		g_form.showFieldMsg('u_effort', 'It should be numeric fractional entry only', 'error');
    } else if (str != newValue) {  // if there was leading or ending spaces, set the value to trimmed value
		g_form.setValue('u_effort', str);
	}
}

Execution result.

1. Enter "      5678     "

find_real_file.png

2. Press tab key.

find_real_file.png

View solution in original post

13 REPLIES 13

Rajababu
Giga Guru

Instead of that you can use simple way to verify if it will fulfill the your requirement because regex is combination of pattern.

Regex can use only for numeric also but below is most recommended. (isNaN)

 

var rajababuVar=g_form.getValue('field name');

if (isNaN(rajababuVar)) //check if value is not numeric

 

-=----------

For Regex you can check on below site 

https://regexr.com/

 

find_real_file.png

Hitoshi Ozawa
Giga Sage
Giga Sage

FYI, about the regular expression. "." is a special character in regular expression so it needs to be escaped. 

var regexp = /^[0-9]*[\.]?[0-9]*$/; // may or may not have fractional. Fractional should also be numeric


if (!regexp.test(newValue))
{
alert('It should be numeric fractional entry only');


g_form.setValue('u_effort', '');
}

or

var regexp = /^\d*[\.]?\d*$/;


if (!regexp.test(newValue))
{
alert('It should be numeric fractional entry only');


g_form.setValue('u_effort', '');
}

The script is working but it is taking space also. Can we do something for that?

 

find_real_file.png

The script show error if there's a space. To allow spaces, just trim() newValue in the test statement. (My bad on the period in regular expression. It's not needed).

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var regexp = /^[0-9]*[.]?[0-9]*$/; // may or may not have fractional. Fractional should also be numeric

    if (!regexp.test(newValue.trim())) {
        alert('It should be numeric fractional entry only');
        g_form.setValue('u_effort', '');
    }
}

The script I've provided prior won't accept spaces. If it is, please check the regular expression.

var regexp = /^[0-9]*[.]?[0-9]*$/;

 

Execution result: (spaces before 5678). "^" won't allow any non-numeric characters at the start and "$" won't allow at the end.

find_real_file.png