01: //The Salmon Open Framework for Internet Applications (SOFIA)
02: //Copyright (C) 1999 - 2002, Salmon LLC
03: //
04: //This program is free software; you can redistribute it and/or
05: //modify it under the terms of the GNU General Public License version 2
06: //as published by the Free Software Foundation;
07: //
08: //This program is distributed in the hope that it will be useful,
09: //but WITHOUT ANY WARRANTY; without even the implied warranty of
10: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11: //GNU General Public License for more details.
12: //
13: //You should have received a copy of the GNU General Public License
14: //along with this program; if not, write to the Free Software
15: //Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16: //
17: //For more information please visit http://www.salmonllc.com
18:
19: package com.salmonllc.examples.example8;
20:
21: import com.salmonllc.sql.DataStoreExpression;
22: import com.salmonllc.sql.DataStoreBuffer;
23: import com.salmonllc.sql.DataStoreException;
24:
25: /**
26: * An object that does simple phone number validation and transformation. It can be attached to an HtmlValidator Text or DataStore validation rule
27: */
28: public class PhoneValidation implements java.io.Serializable,
29: DataStoreExpression {
30: private String _colName;
31: private boolean _required;
32:
33: public PhoneValidation(String colName, boolean required) {
34: _required = required;
35: _colName = colName;
36: }
37:
38: public Object evaluateExpression(DataStoreBuffer dsBuf, int row)
39: throws DataStoreException {
40: String phone = dsBuf.getString(row, _colName);
41: if (phone == null)
42: phone = "";
43: else
44: phone = phone.trim();
45: if (phone.length() == 0) {
46: if (_required)
47: return Boolean.FALSE;
48: else
49: return Boolean.TRUE;
50: }
51:
52: StringBuffer dig = new StringBuffer(phone.length());
53: for (int i = 0; i < phone.length(); i++) {
54: char c = phone.charAt(i);
55: if (Character.isDigit(c))
56: dig.append(c);
57: }
58:
59: if (dig.length() >= 10) {
60: String newPhone = "(" + dig.substring(0, 3) + ") "
61: + dig.substring(3, 6) + "-" + dig.substring(6, 10);
62: if (dig.length() > 10)
63: newPhone += " ex:" + dig.substring(10);
64: if (!newPhone.equals(phone))
65: dsBuf.setString(row, _colName, newPhone);
66: return Boolean.TRUE;
67: }
68:
69: return Boolean.FALSE;
70:
71: }
72:
73: }
|