Probably the easiest way to do this is with javascript. Below is some javascript that uses functions to validate separate things. Just assign each input an ID
AND a name.
At or near the top of the page use
Code:
<?php
if (isset($var))
{
//execute PHP script required (Update, insert etc)
}
else
{
?>
<script language="javascript">
//Copied from martin.f2o.org/javascript/form-validation
//Validates a number
function validateNumber(field, msg, min, max) {
if (!min) { min = 0 }
if (!max) { max = 255 }
if ( (parseInt(field.value) != field.value) ||
field.value.length < min ||
field.value.length > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
//Validates a string
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535 }
if (!field.value || field.value.length < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
//Validates an email address
function validateEmail(email, msg, optional) {
if (!email.value && optional) {
return true;
}
var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
if (!re_mail.test(email.value)) {
alert(msg);
email.focus();
email.select();
return false;
}
return true;
}
</script>
<?php
} //close off else isset("some variable")
?>
---
David Thorne, Student
UK