Field validation Regex in on change client script is allowing incorrect values.

Community Alums
Not applicable

Hi all,

My requirement is I have two fields

Number release field is a choice type (Max, Min, High and low) and NUMBER field is a string

f the user selects "Max" from "Number release" field then the user should only be allowed to enter numerical data without any dots in the NUMBER field
(Eg: Correct:2, Incorrect:2.3, Incorrect: ABC, Incorrect: A.B.C)

Note: All the NUMBER to be captured and stored with three dots in between the digits. To use 0 wherever, necessary.
For eg: Say Major is entered as 15 in the version number field, then it should be stored in the system as 15.0.0.0
Say Minor is entered as 15.2 in the version number field, then it should be stored in the system as 15.2.0.0
Say Maintenance Pack is entered as 15.2.3, then it should be stored in the system as 15.2.3.0
Say Patch is entered as 15.2.3.4, then it should be stored in the system as 15.2.3.4B

 

Below one is my code it is working good without "g_form.clearValue('number'); " this line. Actually this is required to erase or clear incorrect values but it is erasing correct values as well. Without this line code is capturing or allowing incorrect values to saving data. Please help me any alternate way to resolve this is ASAP.

 

var num = g_form.getValue('number');
var release = g_form.getValue('number_release');
var mi = '.' + '0';
var mnpc = '.' + '0' + '.' + '0';
var ptch = '.' + '0' + '.' + '0' + '.' + '0';
try {
// Validate the input based on the selected release type
if (release === 'max') {
// Allow only integers with no dots
if (/^\d+$/.test(num)) {
g_form.setValue('number', newValue + ptch);
} else {
g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid integer with no dots');
}
} else if (release === 'min') {
// Allow integers with one dot
if (/^\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue + mnpc);
} else {
g_form.clearValue('number');
alert('Please enter a valid number with one dot');
}
} else if (release === 'high') {
// Allow integers with two dots
if (/^\d+\.\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue + mi);
} else {
//g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid number with two dots');
}
} else if (release === 'low') {
// Allow integers with three dots
if (/^\d+\.\d+\.\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue);
} else {
g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid number with three dots');
}
}
} catch (ERR) {

}

 

Thanks. 

1 ACCEPTED SOLUTION

@Community Alums try this

var num = g_form.getValue('number');
    var release = g_form.getValue('number_release');
    var min = '.' + '0';
    var mnpc = '.' + '0' + '.' + '0';
    var ptch = '.' + '0' + '.' + '0' + '.' + '0';
    try {
        if (release == 'max') {
            // Allow only integers with no dots
            if (/^\d+$/.test(num)) {
                g_form.setValue('number', newValue + ptch);
            } else if (num.indexOf(ptch) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid integer with no dots');
            }
        } else if (release == 'min') {
            // Allow integers with one dot
            if (/^\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue + mnpc);
            } else if (num.indexOf(mnpc) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                alert('Please enter a valid number with one dot');
            }
        } else if (release == 'high') {
            // Allow integers with two dots
            if (/^\d+\.\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue + min);
            } else if (num.indexOf(min) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid number with two dots');
            }
        } else if (release == 'low') {
            // Allow integers with three dots
            if (/^\d+\.\d+\.\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue);
            }  else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid number with three dots');
            }
        }
    } catch (ERR) {
 
    }
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

View solution in original post

7 REPLIES 7

Community Alums
Not applicable

@BharathChintala

Hi,

My requirement is I have two fields

Number release field is a choice type (Max, Min, High and low) and NUMBER field is a string

f the user selects "Max" from "Number release" field then the user should only be allowed to enter numerical data without any dots in the NUMBER field
(Eg: Correct:2, Incorrect:2.3, Incorrect: ABC, Incorrect: A.B.C)

Note: All the NUMBER to be captured and stored with three dots in between the digits. To use 0 wherever, necessary.
For eg: Say Major is entered as 15 in the version number field, then it should be stored in the system as 15.0.0.0
Say Minor is entered as 15.2 in the version number field, then it should be stored in the system as 15.2.0.0
Say Maintenance Pack is entered as 15.2.3, then it should be stored in the system as 15.2.3.0
Say Patch is entered as 15.2.3.4, then it should be stored in the system as 15.2.3.4B

 

Below one is my code it is working good without "g_form.clearValue('number'); " this line. Actually this is required to erase or clear incorrect values but it is erasing correct values as well. Without this line code is capturing or allowing incorrect values to saving data. Please help me any alternate way to resolve this is ASAP.

 

var num = g_form.getValue('number');
var release = g_form.getValue('number_release');
var mi = '.' + '0';
var mnpc = '.' + '0' + '.' + '0';
var ptch = '.' + '0' + '.' + '0' + '.' + '0';
try {
// Validate the input based on the selected release type
if (release === 'max') {
// Allow only integers with no dots
if (/^\d+$/.test(num)) {
g_form.setValue('number', newValue + ptch);
} else {
g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid integer with no dots');
}
} else if (release === 'min') {
// Allow integers with one dot
if (/^\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue + mnpc);
} else {
g_form.clearValue('number');
alert('Please enter a valid number with one dot');
}
} else if (release === 'high') {
// Allow integers with two dots
if (/^\d+\.\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue + mi);
} else {
//g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid number with two dots');
}
} else if (release === 'low') {
// Allow integers with three dots
if (/^\d+\.\d+\.\d+\.\d+$/.test(num)) {
g_form.setValue('number', newValue);
} else {
g_form.clearValue('number');
// Display an error message if the input is invalid
alert('Please enter a valid number with three dots');
}
}
} catch (ERR) {

}

 

Thanks. 

 

@Community Alums try this

var num = g_form.getValue('number');
    var release = g_form.getValue('number_release');
    var min = '.' + '0';
    var mnpc = '.' + '0' + '.' + '0';
    var ptch = '.' + '0' + '.' + '0' + '.' + '0';
    try {
        if (release == 'max') {
            // Allow only integers with no dots
            if (/^\d+$/.test(num)) {
                g_form.setValue('number', newValue + ptch);
            } else if (num.indexOf(ptch) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid integer with no dots');
            }
        } else if (release == 'min') {
            // Allow integers with one dot
            if (/^\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue + mnpc);
            } else if (num.indexOf(mnpc) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                alert('Please enter a valid number with one dot');
            }
        } else if (release == 'high') {
            // Allow integers with two dots
            if (/^\d+\.\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue + min);
            } else if (num.indexOf(min) > -1) {
                // 
            } else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid number with two dots');
            }
        } else if (release == 'low') {
            // Allow integers with three dots
            if (/^\d+\.\d+\.\d+\.\d+$/.test(num)) {
                g_form.setValue('number', newValue);
            }  else {
                g_form.clearValue('number');
                // Display an error message if the input is invalid
                alert('Please enter a valid number with three dots');
            }
        }
    } catch (ERR) {
 
    }
If my inputs have helped with your question, please mark my answer as accepted solution, and give a thumb up.
Bharath Chintala

Community Alums
Not applicable

@BharathChintala 

Thank you Bharat for resolving issue. It's working fine now.