Using phpformgen I have now created some forms for my site http://www.windsurf.me.uk/Links/links2.html
When you enter data in your selected form this then creates a pointer on a google map displaying the location and some info regarding that point, all read from the database.
Unfortunately the longitude and latitude fields are causing problems for users. Users are ignoring the instructions on how to enter this data in decimal form and are entering it in other forms.
I would like to add some error trapping to the form. It should be quite simple. For longitude the range is -180.00 to 180.00 and for latitude -90.00 to 90.00 so a simple if statement should do it but I cannot seem to work out how to do it.
The form can be found here.
http://www.windsurf.me.uk/Forms/form285/ with the importand bits bein form.html and confirm.html
Any ideas
You could try your hand at using regex to solve your problem or you can take a look over at http://php.net for all the variable functions like is_numeric().
I also recommend you test the input like this:
<?php
$longitude = trim(htmlentities(strip_tags($_POST["Longitude"], ENT_QUOTES, 'UTF-8')));
//Make sure it isn't some long string of XSS or just a huge number...
if (strlen($longitude) > 15) {
print 'Your Longitude is too long...';
} else {
if(!is_numeric($longitude)) {
print 'This is not a valid number.';
} else {
print 'You entered the right number';
}
}
?>Also look at this
It is better to do it in javascript first, then in php (php is safer)...
To the input-field of the longditude add: onchange="checkLongitude(this)", then on the input-field of the latitude add: onchange="checkLatitude(this)".
Then add this code:
<script language="javascript">
function checkLongitude(field)
{
var error = false;
if(isNumeric(field.value))
{
if(!((!(field.value < 180)) && (!(field.value > -180))))
{
error = true;
}
}
else
{
error = true;
}
if(error)
{
field.value = "";
field.focus();
alert("Longitude range is -180.00 to 180.00");
}
}
function checkLatitude(field)
{
var error = false;
if(isNumeric(field.value))
{
if(!((!(field.value < 90)) && (!(field.value > -90))))
{
error = true;
}
}
else
{
error = true;
}
if(error)
{
field.value = "";
field.focus();
alert("Latitude range is -90.00 to 90.00");
}
}
function isNumeric(variable)
{
var ValidChars = "0123456789.";
var IsNumber = true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
</script>