Expand my Community achievements bar.

Simple email validation in forms

Avatar

Level 2

Hello All,

I would like to do a simple email validation, the built in email validation pattern(text{OOOOOOOO'@example.com'} ) is not really useful.

What is the best to validate an email in the form which was built using livecycle designer.

Is there any other pattern I can use?

My requirement is simple, the email should contain ‘@’ and a dot, something like ( xxx@xxx.xxx)

Any help is much appreciated.

Thanks and regards,

6 Replies

Avatar

Level 2

I manage to solve the problem.

It is simple use the custom email address control.

Alternatively place the following javascript in normal textbox control.

// Validate the email address.

var

r = new RegExp("^[a-z0-9_\\-\\.]+\\@[a-z0-9_\\-\\.]+\\.[a-z]{2,3}$"); // Create a new Regular Expression Object.

// Set the regular expression to look for an email address in general form.

var

result = r.test(this.rawValue); // Test the rawValue of the current object to see

// if it fits the general form of an email address.

if

(result == true) // If it fits the general form,

  true; // all is well.

else

// Otherwise,

  false; // fail the validation.

Avatar

Level 5

Perfect -- works like a charm! (LCD ES2).

One question:  is there any way to make the focus stay on the e-mail field?  Because once it produces an error message due to invalid e-mail format, the focus moves to the next field anyway.

Avatar

Level 5

I added a couple of tweaks -- (1) to allow for up to 8-character domain suffixes (there are some long ones out there -- "museum", "travel", etc.);  (2) allows the user to leave the field empty with the initial if statement;  (3) if the field has content, the focus stays on the field if there's a validation error.  Thanks to OP Andrew Smith for the script.


if (this.rawValue != null) {


     // Create a new Regular Expression Object


     var r = new RegExp("^[a-z0-9_\\-\\.]+\\@[a-z0-9_\\-\\.]+\\.[a-z]{2,8}$"); // (allows 2-8 characters in domain suffix)


     // Set the regular expression to look for an email address in general form


     // Test the rawValue of the current object to see if it fits the general form of an email address


     var result = r.test(this.rawValue);


     if (result == false)  {


          xfa.host.messageBox("Invalid e-mail address -- please use format username@domain.xxx","CHECK E-MAIL FORMAT",0,0);


          xfa.host.setFocus(this);


}





Avatar

Level 2

On a text box > Validation expression, add script below

(this.value && this.value.match(/^[A-za-z0-9]+[\\._]*[A-za-z0-9]*@[A-za-z.-]+[\\.]+[A-Za-z]{2,4}$/i) == null) ? false : true

this should solve your problem

Avatar

Level 5

FulufheloMM, could you please explain exactly how that works?