Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Need PHP and JavaScript Form Validation Scripts
Closed Thread
Old 01-05-2008, 05:48 PM   #1 (permalink)
aetherh4cker's Avatar
 
Corrupt Techie

Join Date: Sep 2005

Posts: 752

aetherh4cker is on a distinguished road

Default Need PHP and JavaScript Form Validation Scripts

Basically I want two scripts, one PHP and one JavaScript. These scripts need to validate certain fields in a form to make sure they are in HH:MM:SS format.

I had a paritial JavaScript one, but it doesn't seem to be working at all. The form is a standard form and the submit button looks like this:
Code:
<input type="submit" value="Open Ticket" onClick="return checkForm();" />
Even when I change the functions to only return false, it still submits through.

I also have no idea how this section of code works. I got it from a tutorial, but am not sure how it works. Could anyone point me to a guide on these expression things?
Code:
timePat = /^(\d{1,2}):(\d{2}):(\d{2})$/;
... and this code is all the JavaScript code in my <HEAD>. It doesn't seem to be working, because when a user clicks on Submit the form always posts. Even if I edit the function to only return with false.

Code:
<script language="JavaScript">

		function checkForm() {

			var ctime, ctimeLost, ctimeReran;

			with(window.document.ticket) {
				ctime = time;
				ctimeLost = timeLost;
				ctimeReran = timeReran;
			}

			if(!isValidTime(ctime)) {
				alert("Time Opened much be in HH:MM:SS format");
				return false;
			}

			if(!isValidTime(ctimeLost)) {
				alert("Time Lost much be in HH:MM:SS format");
				return false;
			}

			if(!isValidTime(ctimeReran)) {
				alert("Time Reran much be in HH:MM:SS format");
				return false;
			}

			return true;
		}
		
		function isValidTime(timeStr) {
			// Checks if time is in HH:MM:SS format.
			
			var timePat = /^(\d{1,2}):(\d{2}):(\d{2})$/;
			
			var matchArray = timeStr.match(timePat);

			if (matchArray == null) {
				return false;
			}

			hour = matchArray[1];
			minute = matchArray[2];
			second = matchArray[4];
			
			if (hour < 0  || hour > 23) {
				return false;
			}

			if (minute<0 || minute > 59) {
				return false;
			}

			if (second < 0 || second > 59) {
				return false;
			}
			return true;
		}
		</script>
Thanks for any help.
__________________
aetherh4cker is offline  
Old 01-06-2008, 10:04 AM   #2 (permalink)
Yek
 
T.F's Resident Cool Guy...

Join Date: Aug 2006

Posts: 1,625

Yek is on a distinguished road

Send a message via AIM to Yek Send a message via MSN to Yek Send a message via Yahoo to Yek Send a message via Skype™ to Yek
Default Re: Need PHP and JavaScript Form Validation Scripts

I would reccomend sending a PM to CrazeD , he may be able to help.
Yek is offline  
Old 01-06-2008, 04:16 PM   #3 (permalink)
CrazeD's Avatar
 
Wizard Techie

Join Date: Feb 2006

Location: Maine

Posts: 3,683

CrazeD will become famous soon enough

Send a message via AIM to CrazeD Send a message via MSN to CrazeD
Default Re: Need PHP and JavaScript Form Validation Scripts

This is wrong:

Code:
<input type="submit" value="Open Ticket" onClick="return checkForm();" />
You have to call the function from the <form> element.

Like this:

Code:
<form action="form.html" method="post" onSubmit="return checkForm();">
Hope that helps.
__________________

Need website help? PM me!
CrazeD is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What Is Harder, JavaScript Or PHP? Torner Programming Discussions 8 01-16-2008 08:11 AM
Best way to do form validation? thejeremy Programming Discussions 4 04-17-2007 10:51 PM