使用jquery.validate.min.js无效的Wordpress表单验证

时间:2013-02-11 20:27:04

标签: wordpress jquery-validate validation

我在Wordpress中有一个自定义构建的表单(我考虑使用表单插件,但它是一个3步形式,我看到的插件似乎都不合适)。我正在尝试按照此处的步骤实现表单验证插件:http://www.problogdesign.com/wordpress/validate-forms-in-wordpress-with-jquery/

以下是我在wordpress模板中的内容:

<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>

你会注意到选择中有onchange的调用。该函数计算价格并通过AJAX显示并正常工作。它通过适当的add_action调用在我的插件中排队和本地化。这是lps_calcprice函数的javascript文件:

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

}

这是插件文件,我称之为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);
?>

当我没有在字段中放置任何内容并单击“提交”时,它只会转到下一页。我是Wordpress的新手,所以很可能这是一个简单的修复。我怀疑它与表单操作有关,但我不确定要将其更改为使表单验证工作。当我查看源代码时,我看到头文件中的JS文件,并且我在Firebug中没有出现任何错误。任何意见都将非常感谢!

*注意:我在Wordpress中从php评估器插件中加载时故意删除了状态输入,我不想让它更加混乱。

1 个答案:

答案 0 :(得分:1)

我继续完成了我需要为这个项目完成的其他事情,然后回到验证表单。结果我需要在输入中添加“class = required”。现在就像魅力一样!傻...