3 Replies Latest reply on Aug 20, 2007 6:05 PM by Todd Hough

    How to mask credit card output?

    Todd Hough Newbie

      Does anyone know of an easy way to output credit card numbers so they're masked?

      For example 4222222222222 becomes 4222********2222.

      I'm hoping to find a converter I can attach to an h:outputText element.

      Thanks

        • 1. Re: How to mask credit card output?
          fhh Expert

          Why don't yu simply add another getter to your bean that returns the masked CC number.

          Regards

          Felix

          • 2. Re: How to mask credit card output?
            Matt Drees Master

            Just because I'm probably going to use one at some point...

            import java.io.Serializable;
            
            import javax.faces.component.UIComponent;
            import javax.faces.context.FacesContext;
            import javax.faces.convert.Converter;
            
            import org.jboss.seam.annotations.Logger;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.log.Log;
            
            /**
             * Just for h:outputText components
             * @author Matthew.Drees
             *
             */
            @Name("creditCardMaskingConverter")
            @org.jboss.seam.annotations.faces.Converter
            public class CreditCardMaskingConverter implements Converter, Serializable {
            
             private static final long serialVersionUID = 1L;
            
             @Logger Log log;
            
             String pattern = "1111********1111";
            
             public Object getAsObject(FacesContext context, UIComponent component, String value) {
             throw new UnsupportedOperationException();
             }
            
             public String getAsString(FacesContext context, UIComponent component, Object value) {
             log.trace("getting #0 as String", value);
             if (value == null) {
             throw new IllegalArgumentException("value to convert is null");
             }
             String toString = value.toString();
             assertValidity(toString);
             StringBuilder sb = new StringBuilder();
             for(int i = 0; i < 16; i++) {
             if (pattern.charAt(i) == '*') {
             sb.append('*');
             } else {
             sb.append(toString.charAt(i));
             }
             }
             return sb.toString();
             }
            
             private void assertValidity(String string) {
             if (string.length() != 16) {
             throw new IllegalArgumentException("not a 16-digit string: " + string);
             }
             }
            
             /**
             * A 16-character string indicating the masking pattern. A '*' at index i means
             * the converted output will have a '*' at index i.
             * @param pattern
             */
             public void setPattern(String pattern) {
             assertValidity(pattern);
             this.pattern = pattern;
             }
            
             public String getPattern() {
             return pattern;
             }
            
             public Log getLog() {
             return log;
             }
            
             public void setLog(Log log) {
             this.log = log;
             }
            }
            


            • 3. Re: How to mask credit card output?
              Todd Hough Newbie

              Thanks Matt! That's perfect.