I have a custom built form in WordPress (I considered using a form plugin but it is a 3 step form and none of the plugins I saw seemed suitable). I am trying to implement a form validation plugin following the steps here: http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/
Here’s what I’ve got in my wordpress template:
<form name="lpsOrderForm" id="lpsOrderForm" method="post" class="contact-form" action="/payment-information/">
<input name=action type=hidden value=lps_calc>
<input name=page type=hidden value=2>
<input name=size type=hidden value=2>
<ul class=form-list>
<li><label>Paper</label></li>
<li><select name=paper id=paper onchange="lps_calcprice()">
<option value=4 >70# Gloss</option>
<option value=5 >7 Pt Reply/Uncoated</option>
</select></li>
</ul>
<ul class=form-list>
<li><label>Ink</label></li>
<li><select name=ink id=ink onchange="lps_calcprice()">
<option value=6 >Full Color Both Sides</option>
<option value=7 >Full Color One Side</option>
</select></li>
</ul>
<ul class=form-list>
<li><label>Bleed</label></li>
<li><select name=bleed id=bleed onchange="lps_calcprice()">
<option value=8 >Yes Bleed</option>
<option value=9 >No Bleed</option>
</select></li>
</ul>
<ul class=form-list>
<li><label>Fold</label></li>
<li><select name=fold id=fold onchange="lps_calcprice()">
<option value=0>None</option>
<option value=10 >Half-Fold</option>
<option value=11 >Quarter-Fold</option>
<option value=12 >Tri-Fold</option>
<option value=13 >Double Parallel-Fold</option>
</select></li>
</ul>
<ul class=form-list>
<li><label>Bindery</label></li>
<li><select name=bindery id=bindery onchange="lps_calcprice()">
<option value=0>None</option>
<option value=14 >One Perf</option>
<option value=15 >Two Perfs</option>
<option value=16 >Three Perfs</option>
<option value=17 >Four Perfs</option>
<option value=18 >Five Perfs</option>
<option value=19 >L Perf</option>
</select></li>
</ul>
<ul class=form-list>
<li><label>Quantity</label></li>
<li><select name=quantity id=quantity onchange="lps_calcprice()">
<option value=0>0</option>
<option value=20 >1000</option>
<option value=21 >2000</option>
<option value=22 >3000</option>
<option value=23 >4000</option>
<option value=24 >5000</option>
<option value=25 >10000</option>
<option value=31 >15000</option>
<option value=26 >25000</option>
<option value=27 >50000</option>
<option value=28 >75000</option>
<option value=29 >100000</option>
</select></li>
</ul>
<ul class="form-list">
<li><label>Company/Name:</label></li>
<li><input class="fws4" type="text" name="lps_deliveryname" id="lps_deliveryname"/></li>
</ul>
<ul class="form-list">
<li><label>Address Line 1:</label></li>
<li><input class="fws4" type="text" name="lps_deliveryadd1" id="lps_deliveryadd1"/></li>
</ul>
<ul class="form-list">
<li><label>Address Line 2:</label></li>
<li><input class="fws4" type="text" name="lps_deliveryadd2" id="lps_deliveryadd2"/></li>
</ul>
<ul class="form-list">
<li><label for="city">City:</label></li>
<li><input class="small-tbox" type="text" name="lps_deliverycity" name="lps_deliverycity" id="lps_deliverycity"/></li>
<li><label for="states">State:</label></li>
<li><label for="zip">Zip:</label></li>
<li><input class="small-tbox" type="text" name="lps_deliveryzip" id="lps_deliveryzip"/></li>
</ul>
<input class="checkout-button-blue" type="submit" /></fieldset>
</div>
<div class="clear"></div>
</form>
You’ll notice a call for onchange in the selects. The function calculates a price and displays via AJAX and is working properly. It is enqueued and localized in my plugin with proper add_action calls. Here is the javascript file for the lps_calcprice function:
function lps_calcprice(){
jQuery.post(myAjax.ajaxurl, jQuery("#lpsOrderForm").serialize()
,
function(response_from_the_action_function){
jQuery("#lps_price").html(response_from_the_action_function);
}
);
}
And here is the plugin file, which I call trace_validate_form:
<?php
/**
* Plugin Name: Trace Validate Form
* Description: Validate form data instantly with jQuery. Uses <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Form Validation</a> plugin by Jörn Zaefferer.
* Version: 1.0
* Author: Pro Blog Design
* Author URI: http://www.problogdesign.com/
* Lic
*/
/**
* Add jQuery Validation script on posts.
*/
function trace_vc_scripts() {
// if(is_single() ) {
wp_enqueue_script( "jquery-validate", WP_PLUGIN_URL.'/trace_validate_form/js/jquery.validate.min.js', array('jquery') );
wp_enqueue_style(
'jquery-validate',
plugin_dir_url( __FILE__ ) . 'css/style.css',
array(),
'1.0'
);
// }
}
add_action('init', 'trace_vc_scripts');
/**
* Initiate the script.
* Calls the validation options on the comment form.
*/
function trace_vc_init() { ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#lpsOrderForm').validate({
rules: {
lps_deliveryName: {
required: true,
},
lps_deliveryAdd1: {
required: true
},
lps_deliveryCity: {
required: true
},
lps_deliveryZip: {
required: true,
maxlength: 5,
digits: true
},
},
messages: {
lps_deliveryName: "Please enter a name for the sample/delivery address.",
lps_deliveryAdd1: "Please enter a street address for the sample/delivery address.",
lps_deliveryCity: "Please enter a city for the sample/delivery address.",
lps_deliveryZip: "Please enter a 5 digit zip code for the sample/delivery address."
}
});
});
</script>
<?php }
add_action('wp_footer', 'trace_vc_init', 999);
?>
When I don’t put anything in the fields and click Submit, it just moves on to the next page. I am fairly new to WordPress so its very possible this is a simple fix. I suspect it has something to do with the form action, but I’m not sure what to change it to to get the form validation to work. When I view the source, I see the JS files in the head and I don’t get any errors in Firebug. Any input would be greatly appreciated!
*Note: I removed the state input intentionally as it was loading from inside a php evaluator plugin in WordPress and I didn’t want to make it any more confusing.
I went on to other things I needed to finish for this project and just got back to validating the form. Turns out I needed to add “class=required” to the inputs. Works like a charm now! Silly…