CVS: phpweather/config index.php,1.2,1.3 pw_option_text.php,1.1,1.2 p...
Brought to you by:
iridium
|
From: Martin G. <gim...@us...> - 2002-04-09 23:21:42
|
Update of /cvsroot/phpweather/phpweather/config
In directory usw-pr-cvs1:/tmp/cvs-serv16422
Modified Files:
index.php pw_option_text.php pw_validator.php
pw_validator_ereg.php pw_validator_range.php
Log Message:
Hey - it turned out to be quite easy to validate the input using
regular expression in Javascript...
As far as I understand it, then these regular expressions only work in
Javascript version 1.2 and later. But it's actually not that
important, as this is simply extra functionality - you can still use a
browser like lynx that doesn't do Javascript.
Index: index.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/config/index.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -3 -r1.2 -r1.3
--- index.php 7 Apr 2002 18:53:15 -0000 1.2
+++ index.php 9 Apr 2002 23:21:37 -0000 1.3
@@ -332,6 +332,34 @@
</head>
<body>
+<script language="JavaScript1.2">
+function update(accepted, error, value, output) {
+ if (accepted) {
+ document.getElementById(output).innerHTML = "<font color=\"green\">Input accepted.</font>";
+ } else {
+ while ((index = error.indexOf("%s")) > -1) {
+ error = error.substring(0, index) + value + error.substring(index+2);
+ }
+ document.getElementById(output).innerHTML = "<font color=\"red\">" + error + "</font>";
+ }
+}
+
+function validate(error, value, output, field) {
+ update(field.value == value, error, field.value, output);
+}
+
+function validate_ereg(error, ereg, output, field) {
+ regex = new RegExp(ereg);
+ update(regex.test(field.value), error, field.value, output);
+}
+
+function validate_range(error, low, high, empty_ok, output, field) {
+ update((empty_ok == 1 && field.value == "") ||
+ (field.value >= low && field.value <= high),
+ error, field.value, output);
+}
+</script>
+
<img src="../icons/phpweather-long-white.gif" alt="PHP Weather" align="right">
<h1>Configuration Builder for PHP Weather</h1>
@@ -341,18 +369,17 @@
'Update Options' buttons. Depending on your choices, more options
might appear. Continue to change the options until they all say <span style="color: green">Input accepted.</span></p>
-
<form action="<? echo $PHP_SELF . '?' . SID ?>" method="POST">
<p>You can <input type="submit" name="download" value="Download the Configuration"> or <input type="reset" value="Reset Everything" onclick="document.location='reset_session.php'"></p>
<dl>
<?php
-
+
$general_group->show();
$db_group->show();
$rendering_group->show();
-
+
?>
</dl>
@@ -374,4 +401,4 @@
</form>
</body>
-</html>
\ No newline at end of file
+</html>
Index: pw_option_text.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/config/pw_option_text.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -3 -r1.1 -r1.2
--- pw_option_text.php 7 Apr 2002 18:38:20 -0000 1.1
+++ pw_option_text.php 9 Apr 2002 23:21:37 -0000 1.2
@@ -10,12 +10,15 @@
if ($this->is_ready()) {
echo "<dt>Option <code>$this->name</code>: ";
echo '<input type="text" name="' . $this->name .
- '_value" value="' . htmlentities($this->value) . "\"></dt>\n";
+ '_value" value="' . htmlentities($this->value) .
+ '" onkeyup="' . $this->validator->get_javascript($this->name) .
+ "\"></dt>\n";
echo '<dd><p>' . $this->description . "</p>\n";
if ($this->is_valid()) {
- echo '<p style="color: green">Input accepted.</p>';
+ echo '<p id="' . $this->name . '"><font color="green">Input accepted.</font></p>';
} else {
- echo '<p style="color: red">' . $this->validator->get_error() . '</p>';
+ echo '<p id="' . $this->name . '"><font color="red">' .
+ $this->validator->get_error() . '</font></p>';
}
echo "\n</dd>\n";
}
Index: pw_validator.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/config/pw_validator.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -3 -r1.1 -r1.2
--- pw_validator.php 7 Apr 2002 18:38:20 -0000 1.1
+++ pw_validator.php 9 Apr 2002 23:21:37 -0000 1.2
@@ -18,6 +18,10 @@
return sprintf($this->error, $this->value);
}
+ function get_javascript($id) {
+ return '';
+ }
+
}
Index: pw_validator_ereg.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/config/pw_validator_ereg.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -3 -r1.1 -r1.2
--- pw_validator_ereg.php 7 Apr 2002 18:38:20 -0000 1.1
+++ pw_validator_ereg.php 9 Apr 2002 23:21:37 -0000 1.2
@@ -14,6 +14,11 @@
return ereg($this->regex, $value);
}
+ function get_javascript($id) {
+ return "validate_ereg('" . addslashes($this->error) .
+ "', '$this->regex', '$id', this)";
+ }
+
}
?>
Index: pw_validator_range.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/config/pw_validator_range.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -3 -r1.1 -r1.2
--- pw_validator_range.php 7 Apr 2002 18:38:20 -0000 1.1
+++ pw_validator_range.php 9 Apr 2002 23:21:37 -0000 1.2
@@ -4,21 +4,26 @@
var $low;
var $high;
- var $optional;
+ var $empty_ok;
- function pw_validator_range($error, $low, $high, $optional = false) {
+ function pw_validator_range($error, $low, $high, $empty_ok = false) {
$this->pw_validator($error);
$this->low = $low;
$this->high = $high;
- $this->optional = $optional;
+ $this->empty_ok = $empty_ok;
}
function validate($value) {
$this->value = $value;
- if ($this->optional && empty($value)) return true;
+ if ($this->empty_ok && empty($value)) return true;
return (ereg('^[0-9]+$', $value) && $this->low <= $value && $value <= $this->high);
+ }
+
+ function get_javascript($id) {
+ return "validate_range('" . addslashes($this->error) .
+ "', $this->low, $this->high, $this->empty_ok, '$id', this)";
}
|