02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Including the New File in the Footer<br />

CHAPTER 9 ■ PERFORMING FORM VALIDATION WITH REGULAR EXPRESSIONS<br />

To use the validDate() function, you’ll need to include the new JavaScript file before init.js, so that the<br />

function is available to be called. Do this <strong>by</strong> opening footer.inc.php in the common folder <strong>and</strong> inserting<br />

the following bold code:<br />

<br />

<br />

google.load("jquery", "1");<br />

<br />

<br />

<br />

<br />

<br />

Preventing the Form Submission if Validation Fails<br />

Now that validDate() is available in init.js, you need to add date validation before the form can be<br />

submitted. Store the start <strong>and</strong> end dates in variables (start <strong>and</strong> end, respectively), then check them using<br />

validDate() before allowing the form to be submitted.<br />

Next, modify the click h<strong>and</strong>ler to the Submit button on the form that edits or creates events, <strong>and</strong><br />

then trigger an alert with a helpful error message if either date input has an invalid value. You need to<br />

prevent the form from being submitted as well, so the user doesn’t have to repopulate the other form<br />

fields.<br />

You accomplish this <strong>by</strong> inserting the following bold code into init.js:<br />

// Makes sure the document is ready before executing scripts<br />

<strong>jQuery</strong>(function($){<br />

var processFile = "assets/inc/ajax.inc.php",<br />

fx = {...}<br />

$("li a").live("click", function(event){...});<br />

$(".admin-options form,.admin")<br />

.live("click", function(event){...});<br />

// Edits events without reloading<br />

$(".edit-form input[type=submit]").live("click", function(event){<br />

// Prevents the default form action from executing<br />

event.preventDefault();<br />

// Serializes the form data for use with $.ajax()<br />

var formData = $(this).parents("form").serialize(),<br />

339

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!