Onsubmit client script

Mark Wood
Tera Contributor

Hello Team,

I have implemented the validation for the update set naming convention, and everything is working fine. However, when a user submits the form with a name that does not follow the convention, the validation message appears but disappears within a second.

I would like the message to remain visible for at least 10 seconds to ensure the user has enough time to read it. How can I achieve this?

I have attached my onLoad and onSubmit client scripts for reference.

 

Onload Client Script:

function onLoad() {
 
var fn=g_user.firstName;
var ln=g_user.lastName;
var fv=fn.charAt(0);
var fv2=ln.charAt(0);

if(g_form.isNewRecord())
{
    g_form.setValue('name',"PN-"+fv+fv2+"-"+"STR");
}
   
}

 

onSubmit client script

function onSubmit() {
 
    var name = g_form.getValue('name');
alert("Onsubmit"+name);
    // Define the required pattern: <Project Prefix>-<User Initials>-<Story>
    var pattern =/^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;
 
 
 
    if (!pattern.test(name)) {
        g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
        location.reload();
        return false;
       
    }
   
}

 

1 ACCEPTED SOLUTION

Hi @Mark Wood 
Then just use the same logic in the OnSubmit client script instead of reloading the form.

function onSubmit() {

    var name = g_form.getValue('name');
    alert("Onsubmit" + name);
    // Define the required pattern: <Project Prefix>-<User Initials>-<Story>
    var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;

    if (!pattern.test(name)) {
        g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
        setTimeout(function() {
			g_form.clearValue("name");
			g_form.clearMessages();

            var fn = g_user.firstName;
            var ln = g_user.lastName;
            var fv = fn.charAt(0);
            var fv2 = ln.charAt(0);

            if (g_form.isNewRecord()) {
                g_form.setValue('name', "PN-" + fv + fv2 + "-" + "STR");
            }

        }, 10000);
        return false;


    }

}

View solution in original post

10 REPLIES 10

Chaitanya ILCR
Kilo Patron

Hi @Mark Wood ,

function onSubmit() {

    var name = g_form.getValue('name');
    alert("Onsubmit" + name);
    // Define the required pattern: <Project Prefix>-<User Initials>-<Story>
    var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;



    if (!pattern.test(name)) {
        g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
        setTimeout(function() {
            location.reload();
        }, 3000 /*update the time in ms here*/ );
        // location.reload();
        return false;

    }

}

 

try this on submit client script

I have just added setTimeOut Method update the time in ms as per your requirement

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

J Siva
Tera Sage

Hi @Mark Wood 

You can use setTimeout function to have some delay. PFB

function onSubmit() {
    var name = g_form.getValue('name');
    alert("Onsubmit" + name);
    // Define the required pattern: <Project Prefix>-<User Initials>-<Story>
    var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;

    if (!pattern.test(name)) {
        g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
//Below method will refresh the page after 5 seconds.
//----------------START--------------------- 
        setTimeout(function() {

            location.reload();
        }, 5000);
//------END----------------------
        return false;

    }

}

Hope this helps.
Regards,
Siva

Mark Wood
Tera Contributor

Hello @J Siva 

I have already tried using setTimeout, and it works. However, after 10 seconds, a popup message appears saying:

Reload site? Changes you made may not be saved

I don't want the user to reload the site manually. Instead, I want it to reload automatically 10 seconds after the onSubmit client script error message.

Is there a solution for this?

Hi @Mark Wood 
That's the expected behavior of location.relaod().
Instead of refreshing the window, just clear up the name field on the update set.
So it won't ask the user to confirm anything, at the same time invalid update set name will be cleared off.

function onSubmit() {

    var name = g_form.getValue('name');
    alert("Onsubmit" + name);
    // Define the required pattern: <Project Prefix>-<User Initials>-<Story>
    var pattern = /^[A-Z]{2,3}-[A-Z]{2}-STR\d+$/;

    if (!pattern.test(name)) {
        g_form.addErrorMessage("Invalid Update Set name! Follow the naming convention: Project Prefix-User Initials-Story(e.g., FN-MI-STR00011)");
        setTimeout(function() {    

			g_form.clearValue("name");
			g_form.clearMessages();

        }, 10000);
        return false;


    }

}

 

Regards,
Siva