Ok , let me show you an example
This is one of form validation system i built before . But i can't complete it because i have some problems with array value (test[0] , test[1] , test[2]

, and listbox , and i aslo confused with field id and field name ,

) . This is some kind of template engine
<html>
<body>
<form id=myform action=test.php method=post validation="both">
Hello <input type=text name="test" validation="range:5-10;range-type:string"><br>
Hello <input type=text name="test2" ><br>
<input type=submit>
</form>
</body>
</html>
Will output
Code: Select all
<html>
<body>
<script language=javascript>
function validate_f5c31ba3f53a0b351f422cb01ea4dd2c(form) {
if (!form.getcontrol) form.getcontrol = function(id) {
var ctrl = null;
if ((ctrl = this[id]) || (ctrl = this.all[id])) {
return ctrl;
} else {
return null;
}
}
if (obj = form.getcontrol("test")) {
if (obj.value.length < 5)
{
alert("This field length must be larger than 5 and smaller than 10.");obj.focus();return false
}
if (obj.value.length > 10)
{
alert("This field length must be larger than 5 and smaller than 10.");obj.focus();return false
}
}
return true;
}
</script>
<form id="myform" action="test.php" method="post" onSubmit="return (validate_f5c31ba3f53a0b351f422cb01ea4dd2c(this));" >
<input type=hidden name="__ID__" value="f5c31ba3f53a0b351f422cb01ea4dd2c" />
Hello <input type="text" name="test" id="test" /><br>
Hello <input type="text" name="test2" /><br>
<input type="submit" />
</form>
</body>
</html>
The PHP code
This is the debug version , it looks like a mess
Code: Select all
<?
error_reporting(E_ALL);
session_start();
require "smartValidator.php";
$smartValidator = new smartValidator;
//smartvalidator::initialize();
if ($smartValidator->validate($form))
{
print_r($form);
}
else echo $smartValidator->error();
$smartValidator->set("myform",$form);
$text = <<<EOF
<html>
<body>
<form id=myform action=test.php method=post validation="both">
Hello <input type=text name="test" validation="range:5-10;range-type:string"><br>
Hello <input type=text name="test2" ><br>
<input type=submit>
</form>
</body>
</html>
EOF;
echo smartValidator_output($text);
//echo $text;
?>
In a released version , it should be
Code: Select all
<?
smartValidator::initialize();
$form = array();
if (smartValidator::validate($form,"formid"))
{
//validate ok
}
else
{
//invalid value
smartValidator::set('formid', $form); //set data back to user , and call him to re-enter value
echo "You got error : " . smartValidator::error();
}
?>
<form id=formid action=test.php method=post validation="both">
Hello <input type=text name="test" validation="range:5-10;range-type:string"><br>
Hello <input type=text name="test2" ><br>
<input type=submit>
</form>
And what we gonna do is something similiar to this (we can't use mine , because when i found problem with ID and Name , i tried to fix it and it's a mess now

, but we can reuse some function)