- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2017 02:57 PM
Hi!
I've created an onChange client script to prevent a user from entering anything other than a numeric value in a single line text field. This script is working, but needs a tweak, as it is right now it's not resetting the field to empty as soon as it finds a letter, so for an example entry of "test" it's returning 4 errors in a row, one for each letter. I need it to clear the field after it finds the very first letter. Anyone see what I'm missing? thanks!
Here's the script (credit to Hunter Wolf's previous thread)...thinking I need to edit something. My goal is to alert the user that they can only enter a numeric value, and to have the field clear immediately
- function onChange(control, oldValue, newValue, isLoading) {
- if(!isLoading){
- var val = newValue;
- var achar = '0123456789.'; //Values you want to check for in this have only allowed number but you could to special characters or letters
- var failmsg = 'Enter Here the message you wish to display to the user.';
- //create an array that is all lowercase based on the value if you want this case sensitive remove .toLowerCase()
- var array1 = val.toLowerCase().split('');
- for (var i=0; i < array1.length; i++){
- if (achar.indexOf(array1[i]) >= '0'){
- }
- else{
- alert(failmsg);
- g_form.setValue('disk_space", ''); //clear out the field if value contains a character that does not match.
- }
- }
- }
- }
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2017 03:00 PM
- function onChange(control, oldValue, newValue, isLoading) {
- if(!isLoading){
- var val = newValue;
- var achar = '0123456789.'; //Values you want to check for in this have only allowed number but you could to special characters or letters
- var failmsg = 'Enter Here the message you wish to display to the user.';
- //create an array that is all lowercase based on the value if you want this case sensitive remove .toLowerCase()
- var array1 = val.toLowerCase().split('');
- for (var i=0; i < array1.length; i++){
- if (achar.indexOf(array1[i]) >= '0'){
- }
- else{
- alert(failmsg);
- g_form.setValue('disk_space", ''); //clear out the field if value contains a character that does not match.
- break;
- }
- }
- }
- }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2017 03:00 PM
- function onChange(control, oldValue, newValue, isLoading) {
- if(!isLoading){
- var val = newValue;
- var achar = '0123456789.'; //Values you want to check for in this have only allowed number but you could to special characters or letters
- var failmsg = 'Enter Here the message you wish to display to the user.';
- //create an array that is all lowercase based on the value if you want this case sensitive remove .toLowerCase()
- var array1 = val.toLowerCase().split('');
- for (var i=0; i < array1.length; i++){
- if (achar.indexOf(array1[i]) >= '0'){
- }
- else{
- alert(failmsg);
- g_form.setValue('disk_space", ''); //clear out the field if value contains a character that does not match.
- break;
- }
- }
- }
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2017 03:04 PM
thanks Darius, works perfectly!
only change was the adding of "break;"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2017 05:30 PM
This is a whole lot of lines that really just becomes an unreadable mess. You can use regex matching for quick field validation. It's also best practice to use form messages than alerts, and I wouldn't force clear the field in case it was an errant keystroke.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.hideFieldMsg('disk_space');
return;
}
//Test for only digits in the value
var rx = new RegExp(/^\d+$/);
if (!rx.test(newValue)) {
g_form.hideFieldMsg('disk_space');
g_form.showFieldMsg('disk_space', 'Disk space may only contain digits.', 'error');
} else {
g_form.hideFieldMsg('disk_space');
}
}