How to validate Email field through Client script

Manoj R Zete
Tera Expert

I want to validate Emil id field on Costume form how we can achieve this through Client script?

1 ACCEPTED SOLUTION

Tejal Zete
Giga Expert

Hi Jyoti,

By using below client script you can validate email id on custom form.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 //If the page isn't loading
 if (!isLoading) {
 //If the new value isn't blank
 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');
 }
 
 
 }
 }
}
 
Mark my answer as correct/helpful if it works for you.
 
Regards,
Tejal

View solution in original post

4 REPLIES 4

Tejal Zete
Giga Expert

Hi Jyoti,

By using below client script you can validate email id on custom form.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 //If the page isn't loading
 if (!isLoading) {
 //If the new value isn't blank
 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');
 }
 
 
 }
 }
}
 
Mark my answer as correct/helpful if it works for you.
 
Regards,
Tejal

Thanks a lot 🙂

 

Cheers

GregLusk
Tera Expert

The above works but this is much cleaner and more efficient:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    if (newValue.search(validRegExp) == -1) {
        alert('A valid e-mail address is required eg:-abc@domain.com.');
        g_form.setValue('email', '');
    }
}

Lavanya Marella
Tera Contributor

Hello @GregLusk 

 

Can you help me in altering the regular expression which validates whether the entered email contains @gmail or not.

 

For example abc@outlook.com it should not take. Forward email should only take input as abc@gmail.com. Please  help me.

 

Thanks in advance