| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TRequiredFieldValidator class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TRegularExpressionValidator.php 2550 2008-10-29 19:47:02Z haertl.mike $
|
|
|
10 |
* @package System.Web.UI.WebControls
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Using TBaseValidator class
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Web.UI.WebControls.TBaseValidator');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* TRegularExpressionValidator class
|
|
|
20 |
*
|
|
|
21 |
* TRegularExpressionValidator validates whether the value of an associated
|
|
|
22 |
* input component matches the pattern specified by a regular expression.
|
|
|
23 |
*
|
|
|
24 |
* You can specify the regular expression by setting the {@link setRegularExpression RegularExpression}
|
|
|
25 |
* property. Some commonly used regular expressions include:
|
|
|
26 |
* <pre>
|
|
|
27 |
* French Phone Number: (0( \d|\d ))?\d\d \d\d(\d \d| \d\d )\d\d
|
|
|
28 |
* French Postal Code: \d{5}
|
|
|
29 |
* German Phone Number: ((\(0\d\d\) |(\(0\d{3}\) )?\d )?\d\d \d\d \d\d|\(0\d{4}\) \d \d\d-\d\d?)
|
|
|
30 |
* German Postal Code: (D-)?\d{5}
|
|
|
31 |
* Email Address: \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
|
|
|
32 |
* Japanese Phone Number: (0\d{1,4}-|\(0\d{1,4}\) ?)?\d{1,4}-\d{4}
|
|
|
33 |
* Japanese Postal Code: \d{3}(-(\d{4}|\d{2}))?
|
|
|
34 |
* P.R.C. Phone Number: (\(\d{3}\)|\d{3}-)?\d{8}
|
|
|
35 |
* P.R.C. Postal Code: \d{6}
|
|
|
36 |
* P.R.C. Social Security Number: \d{18}|\d{15}
|
|
|
37 |
* U.S. Phone Number: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
|
|
|
38 |
* U.S. ZIP Code: \d{5}(-\d{4})?
|
|
|
39 |
* U.S. Social Security Number: \d{3}-\d{2}-\d{4}
|
|
|
40 |
* </pre>
|
|
|
41 |
*
|
|
|
42 |
* Note, the validation succeeds if the associated input control contains empty input.
|
|
|
43 |
* Use a {@link TRequiredFieldValidator} to ensure the input is not empty.
|
|
|
44 |
*
|
|
|
45 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
46 |
* @version $Id: TRegularExpressionValidator.php 2550 2008-10-29 19:47:02Z haertl.mike $
|
|
|
47 |
* @package System.Web.UI.WebControls
|
|
|
48 |
* @since 3.0
|
|
|
49 |
*/
|
|
|
50 |
class TRegularExpressionValidator extends TBaseValidator
|
|
|
51 |
{
|
|
|
52 |
/**
|
|
|
53 |
* Gets the name of the javascript class responsible for performing validation for this control.
|
|
|
54 |
* This method overrides the parent implementation.
|
|
|
55 |
* @return string the javascript class name
|
|
|
56 |
*/
|
|
|
57 |
protected function getClientClassName()
|
|
|
58 |
{
|
|
|
59 |
return 'Prado.WebUI.TRegularExpressionValidator';
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* @return string the regular expression that determines the pattern used to validate a field.
|
|
|
64 |
*/
|
|
|
65 |
public function getRegularExpression()
|
|
|
66 |
{
|
|
|
67 |
return $this->getViewState('RegularExpression','');
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* @param string the regular expression that determines the pattern used to validate a field.
|
|
|
72 |
*/
|
|
|
73 |
public function setRegularExpression($value)
|
|
|
74 |
{
|
|
|
75 |
$this->setViewState('RegularExpression',$value,'');
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* This method overrides the parent's implementation.
|
|
|
80 |
* The validation succeeds if the input data matches the regular expression.
|
|
|
81 |
* The validation always succeeds if ControlToValidate is not specified
|
|
|
82 |
* or the regular expression is empty, or the input data is empty.
|
|
|
83 |
* @return boolean whether the validation succeeds
|
|
|
84 |
*/
|
|
|
85 |
public function evaluateIsValid()
|
|
|
86 |
{
|
|
|
87 |
if(($value=$this->getValidationValue($this->getValidationTarget()))==='')
|
|
|
88 |
return true;
|
|
|
89 |
if(($expression=$this->getRegularExpression())!=='')
|
|
|
90 |
{
|
|
|
91 |
$mods = $this->getPatternModifiers();
|
|
|
92 |
return preg_match("/^$expression\$/{$mods}",$value);
|
|
|
93 |
}
|
|
|
94 |
else
|
|
|
95 |
return true;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* @param string pattern modifiers for server side validation,
|
|
|
100 |
* see http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
|
|
|
101 |
*/
|
|
|
102 |
public function setPatternModifiers($value)
|
|
|
103 |
{
|
|
|
104 |
$this->setViewState('PatternModifiers', $value);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* @return string pattern modifiers, no modifiers by default.
|
|
|
109 |
*/
|
|
|
110 |
public function getPatternModifiers()
|
|
|
111 |
{
|
|
|
112 |
return $this->getViewState('PatternModifiers', '');
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* @param string pattern modifiers for clientside.
|
|
|
117 |
* (Only 'g','i' and 'm' are available.)
|
|
|
118 |
*/
|
|
|
119 |
public function setClientSidePatternModifiers($value)
|
|
|
120 |
{
|
|
|
121 |
$this->setViewState('ClientSidePatternModifiers', $value);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* @return string clientside pattern modifiers, no modifiers by default.
|
|
|
126 |
*/
|
|
|
127 |
public function getClientSidePatternModifiers()
|
|
|
128 |
{
|
|
|
129 |
return $this->getViewState('ClientSidePatternModifiers', '');
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Returns an array of javascript validator options.
|
|
|
134 |
* @return array javascript validator options.
|
|
|
135 |
*/
|
|
|
136 |
protected function getClientScriptOptions()
|
|
|
137 |
{
|
|
|
138 |
$options = parent::getClientScriptOptions();
|
|
|
139 |
$options['ValidationExpression']=$this->getRegularExpression();
|
|
|
140 |
$options['PatternModifiers']=$this->getClientSidePatternModifiers();
|
|
|
141 |
return $options;
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|