limit the decimal value in the integer field

chandan31
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldName = g_form.getValue('hours');   //use your field name

    // Regular expression to match the desired format (maximum 39 hours and one decimal place)
    var regex = /^([1-9]|[1-3][0-9])(\.\d)?$/;

    // Check if the value matches the regex pattern
    if (!regex.test(fieldName)) {
        // If the value doesn't match, show an error message

        alert('Please enter a valid number with one decimal place, maximum 39 hours.');
        // Set the field value to an empty string or another default value

        g_form.setValue('hours', '');
    }
    //Type appropriate comment here, and begin script below
}
 
This script is runinng correctly from 0 to 39.9 after that  it is throwing the error message but now i want that 0 to 39.0 after that it should throw the error message.
 
Prashant Ahire
@Prashant Ahire Can you help me 
 
Thank ,
 
Chandan
 
1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

If its not working then you could simply put an extra if condition to validate the value should be less than  39

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldName = g_form.getValue('hours');   //use your field name

 

    // Regular expression to match the desired format (maximum 39 hours and one decimal place)
    var regex = /^([1-9]|[1-3][0-9])(\.\d)?$/;

 

    // Check if the value matches the regex pattern
    if (!regex.test(fieldName) || fieldName > 39) {
        // If the value doesn't match, show an error message

 

        alert('Please enter a valid number with one decimal place, maximum 39 hours.');
        // Set the field value to an empty string or another default value

 

        g_form.setValue('hours''');
    }
    //Type appropriate comment here, and begin script below
}

View solution in original post

3 REPLIES 3

Gurpreet07
Mega Sage

Hi, 

Try below regex 

var regex = /^([0-3][0-9])(\.\d)?|0$/;

it is not working not giving error message in 39.1

Gurpreet07
Mega Sage

If its not working then you could simply put an extra if condition to validate the value should be less than  39

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var fieldName = g_form.getValue('hours');   //use your field name

 

    // Regular expression to match the desired format (maximum 39 hours and one decimal place)
    var regex = /^([1-9]|[1-3][0-9])(\.\d)?$/;

 

    // Check if the value matches the regex pattern
    if (!regex.test(fieldName) || fieldName > 39) {
        // If the value doesn't match, show an error message

 

        alert('Please enter a valid number with one decimal place, maximum 39 hours.');
        // Set the field value to an empty string or another default value

 

        g_form.setValue('hours''');
    }
    //Type appropriate comment here, and begin script below
}