Data Validation Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
blkshirt
Forum Newbie
Posts: 11
Joined: Sat Dec 06, 2003 8:40 pm
Location: Ohio

Data Validation Question

Post by blkshirt »

Anybody know a way that I could do some data validation???

Here's the problem...

Pages are using php connected to a mysql db.

There is a drop down list box that will list Track and Field events (ex. 100 Meter Dash, Long Jump, 4x100m Relay, etc). The list box is populated from the database, because each meet can have different events.

Track and Field events are of specific type (Track, Field or Relay) this will help in the data validation and I use this in another place as well, in the site.

Question... Once a coach selects a specific event (we'll say the Long Jump) from the list box, is there any way that I can figure out what event is selected. Here's what I want to do in pseudocode...

Code: Select all

Find selection by coach 
   Connect to db and find type of event 
       If type "track" then 
            do data validation for track event 
            If Incorect
                Prompt User
            Else
                 Accept Input
            End If
       End If 

       If type "relay" then 
             do data validation for relay event 
                   If Incorect
                      Prompt User
                   Else
                      Accept Input
                    End If
       End If
 
       If type "field" then 
             do data validation for field event 
                If Incorect
                    Prompt User
                Else
                    Accept Input
                End If
       End if



All entries would be of type string or varchar (depending how you look at it) You can't put it the inputs as of type Int because you may have to put times like 2:05.44 for the 800m this won't work because of the : and you can't use it for field events either because the format needs to be feet-inch.nearest quarter inch (22-11.75, for example)

Anybody have any suggestions?????
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

hmm

Post by mwong »

So let me see if I have this straight. You have different events for the drop down. Each event belongs to a category (field, track, relay).

Each event has it's own way of entering data. You're problem is that you want to show the appropriate data entry format per event selected by the user???
blkshirt
Forum Newbie
Posts: 11
Joined: Sat Dec 06, 2003 8:40 pm
Location: Ohio

Re: hmm

Post by blkshirt »

mwong wrote:So let me see if I have this straight. You have different events for the drop down. Each event belongs to a category (field, track, relay).

Each event has it's own way of entering data. You're problem is that you want to show the appropriate data entry format per event selected by the user???
Correct....
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Heres an example I've used.

form.html

Code: Select all

<form action="validate.php" method="post">
<select name="number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
validate.php

Code: Select all

<?php
if ($_POST['number'] == "1")
{
    echo "You picked number 1!";
}
elseif ($_POST['number'] == "2")
{
    echo "You picked number 2!";
}
elseif ($_POST['number'] == "3")
{
    echo "You picked number 3!";
}
//or alternatively
switch ($_POST['number']) {
   case 1:
       echo "You picked number 1!";
       break;
   case 2:
       echo "You picked number 2!";
       break;
   case 3:
       echo "You picked number 3!";
       break;
   default:
       echo "You picked a different number then 1,2 or 3!";
       break;
}
?>
Try editing it to do what you want to do.
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Hmm

Post by mwong »

The suggestion blkshirt posted above will work if you have two pages......if you wanted the form to change instantly then you would have to use javascript or some kind of client-side script....which I know nothing about. =0)
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Post by mwong »

whups....I meant DufF
blkshirt
Forum Newbie
Posts: 11
Joined: Sat Dec 06, 2003 8:40 pm
Location: Ohio

Post by blkshirt »

Here's the javascript code that's been worked on... There are three buttons.. One for running events, one for field events and one for metric (some meets require field events to be in metric format).

Code: Select all

<script language="javascript">

function TrackValidation()
&#123;
	var num=document.jsvalidation.txtInput.value;
	var myRegExp=/&#1111;^$0-9\:\.]/;
	var charExist=num.search(myRegExp);
	var colonPosi=num.search(/\:/);
	var dotPosi=num.search(/\./);

	if (num.length==0)
	&#123;
		alert("enter a valid input!");
		return false;
	&#125;

	if (charExist>=0)
	&#123;
		alert("enter a valid number!");
		return false;
	&#125;
	if (colonPosi>=0)
	&#123;
		var minutes=num.substring(0, colonPosi);
		if (minutes>60)
		&#123;
			alert("minute must be less than equal to 60!");
			return false;
		&#125;
	&#125;
	if (dotPosi>=0)
	&#123;
		if (colonPosi>=0)
		&#123;
			var seconds=num.substring(colonPosi+1, dotPosi);
			var miliseconds=num.substring(dotPosi+1, num.length);

			if (miliseconds>0)
			&#123;
				if (seconds>59)
				&#123;
					alert("seconds must be less than equal to 59!");
					return false;
				&#125;
			&#125;
			if (miliseconds>60)
			&#123;
				alert("miliseconds must be less than equal to 60!");
				return false;
			&#125;
			if (miliseconds==-1)
			&#123;
				if (seconds>60)
				&#123;
					alert("seconds must be less than equal to 60!");
					return false;
				&#125;
			&#125;
		&#125;
		else if (colonPosi==-1)
		&#123;
			var seconds=num.substring(0, dotPosi);
			var miliseconds=num.substring(dotPosi+1, num.length);

			if (miliseconds>0)
			&#123;
				if (seconds>59)
				&#123;
					alert("seconds must be less than equal to 59!");
					return false;
				&#125;
			&#125;
			if (miliseconds>60)
			&#123;
				alert("miliseconds must be less than equal to 60!");
				return false;
			&#125;
			if (miliseconds==-1)
			&#123;
				if (seconds>60)
				&#123;
					alert("seconds must be less than equal to 60!");
					return false;
				&#125;
			&#125;
		&#125;
	&#125;

	alert("value is valid!");
	return true;
&#125;

function FieldValidation()
&#123;
	var num=document.jsvalidation.txtInput.value;
	var myRegExp=/&#1111;^$0-9\-\.]/;
	var charExist=num.search(myRegExp);
	var dashPosi=num.search(/\-/);
	var dotPosi=num.search(/\./);
	var lastDigit=num.substring(dotPosi+1,num.length);
	var remainder=lastDigit % 25;

	if (num.length==0)
	&#123;
		alert("enter a valid input!");
		return false;
	&#125;

	if (charExist>=0)
	&#123;
		alert("enter a valid number!");
		return false;
	&#125;
	if ((dashPosi!=3 || dotPosi!=6) && (dashPosi!=2 || dotPosi!=5))
	&#123;
		alert("number format must be ##-##.## or ###-##.##!");
		return false;
	&#125;
	if (remainder!=0)
	&#123;
		alert("decimal value must be multiple of 25!");
		return false;
	&#125;

	alert("value is valid!");
	return true;
&#125;

function MetricsValidation()
&#123;
	var num=document.jsvalidation.txtInput.value;
	var myRegExp=/&#1111;^$0-9\.]/;
	var charExist=num.search(myRegExp);
	var dotPosi=num.search(/\./);

	if (num.length==0)
	&#123;
		alert("enter a valid input!");
		return false;
	&#125;

	if (charExist>=0)
	&#123;
		alert("enter a valid number!");
		return false;
	&#125;
	if (dotPosi>=0)
	&#123;
		var unit=num.substring(0, dotPosi);

		if (unit>299)
		&#123;
			alert("maximum value permissible is 299.99!");
			return false;
		&#125;

		var decimal=num.substring(dotPosi+1, num.length);

		if (decimal=="")
		&#123;
			alert("enter a decimal value!");
			return false;
		&#125;

		if (decimal>99)
		&#123;
			alert("enter a valid decimal value!");
			return false;
		&#125;
	&#125;
	if (dotPosi==-1)
	&#123;
		alert("enter a decimal value!");
		return false;
	&#125;

	alert("value is valid!");
	return true;
&#125;

</script>
I need it to justify inches, but that isn't a big deal.. I'm just curious if there is a way to take this script and be able to call back to the db using some sort of connection string and check what type of event the selected event is....
Post Reply