This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Mar 4, 2012, 3:16 AM
63 Posts

Xpages and Java Email Validator (JavaBean)

  • Category: Custom JSF code (Eclipse IDE)
  • Platform: Windows
  • Release: 8.5.2
  • Role:
  • Tags: Xpages,JavaBean,Backing Bean,Validator
  • Replies: 3

Xpages

Xpages and Java Email Validator

 

Knowledge Sharing:

Perhaps this has been done but I could not find an option to validate email addresses where the address is a 'you@yourcompany.state.ny.us type address in this case, for one of our Xpages Applications.  The built-in email validator pattern seemed to recognize the specific email as invalid, modifying the pattern to add sample similarly in below JavaBean did not seem to work either. 

I was compelled to research and write a bit of Java code to do the validation and plug into our App, looks like a better option thus far.   Please add comments to this post if I've missed it and there is indeed a way to go about this right from Xpages.  I must say this was quite the learning experience to do it strictly from Java code. 

Brief outline:

1. Write a JavaBean

2. Implement Validator, expose in faces config

3. Build three Xpages forms

4. Submit data to NSF back-end

 
SubmissionJavaBean.java

[CODE]

 

/**

* Program: SubmissionJavaBean.java

* Created: 2010.12.30.2.30.PM

* New Submission JavaBean code for Xpages form

* Modified: 2012.02.14.10.00.AM

* Added Email Validator

*/

package com.dokoll.solutions.inc.JavaBean;

/**

* @author Dököll Solutions, Inc.

* @version 2010.12.30.2.30.PM

*/

import java.io.IOException;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.faces.context.FacesContext;

import lotus.domino.NotesException;

import lotus.domino.local.Database;

import lotus.domino.local.Document;

//Faces message for email validation/confirmation

//2012.02.08.2.03.PM

import javax.faces.application.FacesMessage;

import javax.faces.component.UIComponent;

import javax.faces.validator.Validator;

import javax.faces.validator.ValidatorException;

import javax.servlet.http.HttpSession;

@FacesValidator(value="SubmissionJavaBean")

public class SubmissionJavaBean implements Validator{

// declare variables

private String LastName;

private String FirstName;

private String EmailAddress;

// Added email confirmation

// @author: Köll S. Cherizard

// @version 2012.02.08.1.56.PM

private String EmailConfirm;

 

/**

* @return the lastName

*/

public String getLastName() {

return LastName;

}

/**

* @param lastName

* the lastName to set

*/

public void setLastName(String lastName) {

LastName = lastName;

}

/**

* @return the firstName

*/

public String getFirstName() {

return FirstName;

}

/**

* @param firstName

* the firstName to set

*/

public void setFirstName(String firstName) {

FirstName = firstName;

}

/**

* @return the emailAddress

*/

public String getEmailAddress() {

return EmailAddress;

}

/**

* @param emailAddress the emailAddress to set

*/

public void setEmailAddress(String emailAddress) {

EmailAddress = emailAddress;

}

/**

* @return the emailConfirm

*/

public String getEmailConfirm() {

return EmailConfirm;

}

/**

* @param emailConfirm

* the emailConfirm to set

*/

public void setEmailConfirm(String emailConfirm) {

EmailConfirm = emailConfirm;

}

//NOTE: this email validation works best with Xpages

//works where address is a @yourcompany.state.ny.us type

//the built-in option, as far as I can see works only with @yourcompany.com, .net, and so on...

//2012.02.14.10.00.AM

public void validate(FacesContext facesContext, UIComponent uIComponent,

Object object) throws ValidatorException {

// @FacesValidator(value="SubscriptionBean") is needed up top

// 2012.02.14.10.00.AM

EmailConfirm = (String) object;

EmailAddress = (String) object;

// TODO modify built-in pattern to add below characters within Xpages

//2012.02.14.10.23.AM

Pattern p = Pattern.compile(".+@.+\\.[a-z]+");// \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Matcher cm = p.matcher(EmailConfirm);

Matcher em = p.matcher(EmailAddress);

boolean matchCFound = cm.matches();

boolean matchEFound = em.matches();

if (!matchCFound || !matchEFound) {

FacesMessage message = new FacesMessage();

// load message to Xpages form

// 2012.02.14.9.40.AM

message.setSummary("Invalid Email ID");

throw new ValidatorException(message);

}

}

//button code

public void submitEntry() {

// let's add a try catch here

try {

// get the current database being used

Database database = (Database) FacesContext.getCurrentInstance()

.getApplication().getVariableResolver().resolveVariable(

FacesContext.getCurrentInstance(), "database");

//document create call

Document submitDocument = (Document) database.createDocument();

// submit using Instructions form

submitDocument.appendItemValue("form", "ContactForm");

// using appendItemValue to insert in the fields needed

// notice we are referencing the field on the xpage, and the

// JavaBean variables

submitDocument.appendItemValue("lastName", LastName);

submitDocument.appendItemValue("firstName", FirstName);

submitDocument.appendItemValue("emailAddress", EmailAddress);

//Email Confirm catch: DO NOT let user submit form if email does not match

//2012.02.14.10.00.AM

if (EmailAddress.equalsIgnoreCase(EmailConfirm)) // {

try {

//load successful page ONLY if user email confirmation checks out okay

//2012.02.14.10.00.AM

FacesContext.getCurrentInstance().getExternalContext()

.redirect("../docu.nsf/xpsuccessful.xsp");// ./index.html

//2012.02.29.3.00.PM

// TODO Clear session so user does not re-enter data by hitting back button on Successful page

//Added session Invalidate code

//2012.03.02.2.21.PM

FacesContext facesContext = FacesContext.getCurrentInstance();

HttpSession httpSession = (HttpSession)facesContext.getExternalContext().getSession(false);

httpSession.invalidate();

//saves the data, based on above fields to Form and ONLY if user email confirmation checks out okay

//2012.02.14.10.00.AM

submitDocument.save();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

else

// try connecting to the confirm email Xpages form

// should carry over the data from the previous page

try {

// call FacesContext to redirect to page in question

//give user option to re-enter email

FacesContext.getCurrentInstance().getExternalContext()

.redirect("../docu.nsf/xpconfirmemail.xsp");

//DO NOT save this form, Unless email confirm works, send user back to try again

return;

} catch (IOException ex) {

}

// clean up the system

submitDocument.recycle();

// throwable initialized if there is an error, either in the field

// or the query

} catch (NotesException e) {

// print this error to the server

e.printStackTrace();

}

}

}

 
[/CODE] 

TIP: EmailConfirm field does not need to get saved in the App, meant only to confirm on the page

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

Here is the main Xpages form, only adding the tableRow info here, areas of interest have been highlighted for your convenience

xpcontactform.xsp

[CODE]

<xp:tr style="font-family:Verdana">

<xp:tr

style="font-family:Verdana">

<xp:td colspan="3"

style="background-color:rgb(220,220,220);font-size:9pt;font-weight:bold;font-family:Verdana">

<br></br>

&#160;*Indicates Required

Information

</xp:td>

</xp:tr>

<xp:td

style="background-color:rgb(220,220,220);font-size:9pt;width:166.0px;font-family:Verdana">

&#160;

<xp:span

style="font-weight:bold;font-family:Verdana" />

<xp:span

style="font-weight:bold;font-family:Verdana">

</xp:span>

&#160;&#160;&#160;

<xp:br></xp:br>

<xp:br

style="font-family:Verdana">

</xp:br>

&#160;&#160;&#160;

<xp:label value="Last Name *"

id="label1" for="lastName1" />

<xp:br></xp:br>

&#160;&#160;

<xp:inputText

value="#{SubmissionJavaBean.lastName}" id="lastName1"

required="true" disableClientSideValidation="true"

style="width:118.0px">

<xp:this.validators>

<xp:validateRequired

message="Enter Last Name" />

</xp:this.validators>

</xp:inputText>

<xp:br />

&#160;&#160;

<xp:message for="firstName1" />

</xp:td>

<xp:td

style="background-color:rgb(220,220,220);font-size:9pt;width:163.0px">

&#160;&#160;&#160;

<xp:br></xp:br>

<xp:br></xp:br>

&#160;

<xp:label value="First Name *"

id="label2" for="firstName1" />

 

&#160;&#160;

<xp:inputText

value="#{SubmissionJavaBean.firstName}" id="firstName1"

required="true" disableClientSideValidation="true"

style="width:118.0px">

<xp:this.validators>

<xp:validateRequired

message="Enter First Name"/>

</xp:this.validators>

</xp:inputText>

<xp:br />

&#160;&#160;

<xp:message for="firstName1" />

</xp:td>

 

 

 

 

 

</xp:tr>

<xp:tr>

<xp:td colspan="2"

style="background-color:rgb(220,220,220);font-size:9pt">

&#160;&#160;&#160;

<xp:label

value="E-Mail Address *" id="emailAddress_Label1"

for="emailAddress1" />

<xp:br id="br16" />

&#160;&#160;

<xp:inputText id="emailAddress1"

style="width:275.0px" required="true"

value="#{SubmissionJavaBean.emailAddress}"

disableClientSideValidation="true">

<xp:message for="emailAddress1" />

 
<xp:this.validators>

<!--<xp:validateConstraint

message="Please enter in 'you@yourcompany.com' format"

loaded="false">

<xp:this.regex><![CDATA[^[A-Za-z0-9._%-]*@[A-Za-z0-9]*.[A-Za-z]{2,4}]]></xp:this.regex>

</xp:validateConstraint>

-->

 

<xp:validator

validatorId="SubmissionJavaBean">

 
</xp:validator>

 

<xp:validateRequired

message="Enter E-Mail address" />

</xp:this.validators>

</xp:inputText>

<xp:br />

&#160;&#160;

<xp:message for="emailAddress1" />

<xp:br></xp:br>

&#160;&#160;&#160;

<xp:label

value="Confirm E-Mail *)" id="label5"

for="emailConfirm1" />

<xp:br id="br1" />

&#160;&#160;

<xp:inputText id="emailConfirm1"

style="width:275.0px" required="true"

value="#{SubscriptionBean.emailConfirm}"

disableClientSideValidation="true">

 

 
<xp:this.validators>

<!--<xp:validateConstraint

message="Please enter in 'you@yourcompany.com' format">

<xp:this.regex><![CDATA[^[A-Za-z0-9._%-]*@[A-Za-z0-9]*.[A-Za-z]{2,4}]]></xp:this.regex>

</xp:validateConstraint>-->

 

<xp:validator

validatorId="SubmissionJavaBean">

 
</xp:validator>

<xp:validateRequired

message="Confirm E-Mail address" />

 
</xp:this.validators>

</xp:inputText>

<xp:br />

&#160;&#160;

<xp:message for="emailConfirm1" />

</xp:td>

</xp:tr>

[/CODE]

TIP: Be sure to reference the JavaBean and the validator in faces config file

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>

face-config.xml

[CODE]

<managed-bean>    

<managed-bean-name>SubmissionJavaBean

</managed-bean-name>    

<managed-bean-class>

com.dokoll.solutions.inc.JavaBean.SubmissionJavaBean

</managed-bean-class>    

<managed-bean-scope>session</managed-bean-scope>

</managed-bean>

 

<validator>

<validator-id>SubmissionJavaBean</validator-id>

<validator-class>com.dokoll.solutions.inc.JavaBean.SubmissionJavaBean

</validator-class>

</validator>

[/CODE]

Conclusion:

Build successful and email confirmation forms.  Save and fire the application...
Happy Coding!
 
Version: 2012.03.03.9.17.PM
Mar 4, 2012, 10:54 AM
56 Posts
Re: Xpages and Java Email Validator (JavaBean)
Hi,
 
thanks for the code, but I think OpenNTF (http://www.openntf.org/Internal/home.nsf), or XSnippets (http://openntf.org/XSnippets.nsf/home.xsp) are a better place to share something like that =)
 
 
Mar 5, 2012, 2:21 AM
63 Posts
Re: Xpages and Java Email Validator (JavaBean)
Quite welcome Matthias, thanks for saying that, been meaning to do that...  Have a great week!
 
Köll 
Mar 5, 2012, 7:49 PM
129 Posts
Re: Xpages and Java Email Validator (JavaBean)
You could do this in a single line, with an expression validator (unless I missed something). Expression validators use regular expression.
 
 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal