onChange script for stop record submission if the validation doesn't meet
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2024 04:29 AM
Hi Community,
I have a requirement that the field length should be minimum 25 characters with no blank space. If the validation doesn't meet an alert message should pop-up and shouldn't allow to save the record. I have written a onChange script, it is showing the pop-up message however it is allowing to save the record. Please help me in how to restrict to save the record if the validation doesn't meet?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2024 07:00 PM
Hi @JaganN971616731 ,
try below code, you can use the gs.addErrorMessage() function to display an error message and return false to prevent the record from being saved
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var char = newValue.toString();
var str = char.replace(/ /g, "");
if (str.length < 25) {
gs.addErrorMessage('Problem statement should have minimum 25 characters excluding spaces.');
return false; // Prevent saving the record
}
}
If my response helped you, please click on "Accept as solution" and mark it as helpful.
Thanks
Suraj!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2024 03:31 AM
Hi @JaganN971616731 ,
You are using onChange Client script, that won't help to stop the form form submitting, instead you can use the same code logic in onSubmit client script. you just have to change a logic little bit, instead of using newValue you will have to get the value of the desired field using getValue.
var char = g_form.getValue('my_field').toString(); // add the backend name of the field in place of "my_field".
var str = char.replace(/ /g, "");
if (str.length < 25) {
alert('Problem statement should have minimum 25 characters excluding spaces.');
//============================================================
return false;
//============================================================
}
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2024 01:11 AM
Hello @JaganN971616731
Please try below script using onsubmit client script and I have tried and it works .please try it and let me know.