Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorial Index AK Pro Photoshop Lab Capital Tutorials 

Change Language

Who's online

There are currently 0 users and 30 guests online.

Error trap

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


Submitted by phill104 on June 24, 2007 - 9:31pm.
printer friendly version

Using Regex and other things

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';
    }

}
?>

Of course this is just the simplest mock-up I could do without any effort or knowledge of how you process your code.

Also look at this


Javascript

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>

Alxandr


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati