Is there a way to validate an email address

Cupcake
Mega Guru

I want to be able to validate if an email address is in the correct format.

Validate this is valid email address xxxxx@xxx.xxx - if not valid email, pop "Please enter valid email address"

Is there a way to do that?

Thanks,

Karen

1 ACCEPTED SOLUTION

Ian Mildon
Tera Guru

There is an onSubmit client script included OOB with HR Service Management that has the following validation script:

function onSubmit() {
   //Type appropriate comment here, and begin script below
   g_form.hideErrorBox('email');
   return validateEmail('email');
}

function validateEmail(field) { 
		var email = g_form.getValue(field);
		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)) {
			g_form.showErrorBox(field, getMessage('Invalid Email'));
			return false;
		}
		return true;
	}

You should be able to reuse this in a new client script for your issue.

View solution in original post

11 REPLIES 11

Thank you I figured that part out after I replied back.

Much appreciated. This was my learn new item today.

 

Enjoy.

Thank u very very much. I struggled 2 days for this task. Finally when i see this code tried working. Many thanks.

Cupcake
Mega Guru

For anyone looking for a solution to this.

In addition to the solution that Ian provided. I added an extra line of code so that it would provide a pop-up message asking to enter a Valid Email Address.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') return;
function validateEmail ( 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,}))$/;
return re.test( email.toLowerCase() );
}
if ( !window ) { // Only run in Service Portal
g_form.hideFieldMsg( 'wsa_email', 'true' ); // Hide any existing field messages
if ( !(validateEmail( newValue )) ) {
g_form.showFieldMsg( 'wsa_email', 'Invalid email address: ' + newValue, 'error', false );
alert('Please provide a valid email address');
g_form.setValue( 'wsa_email', '' ); // Clear out the invalid value
}
}
}

Thanks for taking time to post this!

 

Rick Forristall
Tera Guru

One thing I've always found a bit of a challenge is when I enter an incorrect email address and all I get back is a "slap on the hand" error message and then the email field clears out. Then I have to retype everything.

So, when I produce a showErrorBox(), I insert the value the user entered so they can copy/paste it back into the email field and make corrections rather than have to retype the entire thing.

I'm doing this now in a current project for all my showErrorBox() messages (where I clear out the field value).

var email = newValue.trim();
var isValidEmail = validateEmail(email);

if (!isValidEmail) {
	g_form.setValue('u_email', '');
	g_form.showErrorBox('u_email', "Please enter a valid email address. You entered: " + email);
}

find_real_file.png