How to stop the form submission from onchange Client Script

Pravalika1
Tera Contributor

Hi Experts,

 

I have a requirement when user enters invalid email pattern and tries to save the form, it should abort the form submission.

Below is my script Please correct it to make to work.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var email = g_form.getValue('email');
if (!email || email.trim().length <= 0)
return true;
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (re.test(email)) {
return true;
}

g_form.showFieldMsg('email', 'Invalid Email', 'error');
return false;
}

 

Thank you,

3 REPLIES 3

mdash
Giga Guru

Hi,

This should work for you.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var email = g_form.getValue('email');

    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    var isValidFormat = re.test(email);

    if (!isValidFormat) {

        g_form.showFieldMsg('email', 'Invalid Email', 'error');

    }
}


Please mark my answer as helpful/correct depending on its impact.

Thank You

Dan20
Tera Contributor

This didn't work for me. Do you have any idea why?

 

It doesn't style like an error should or the docs say. If I use 'info' instead of 'error', it styles fine and works as expected for info.

Pooja Mallikarj
Kilo Sage

Hi,

Try with below script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 //If the page isn't loading
 if (!isLoading)
{
 if(newValue != '')
{
var oldV = oldValue;
 var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
var strEmail = g_form.getValue('u_company_official_email_id');   //here u_company_official_email_is is my field actual name 
if (strEmail.search(validRegExp) == -1)
 {
 alert('A valid e-mail address is required eg:-abc@domain.com.');
 g_form.setValue('u_company_official_email_id','oldV');
 }
 }
 }
}
 
Please mark correct/helpful if this works for you.
 
Thanks,
Pooja M