首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter\表达式引擎regex表单验证无效

CodeIgniter\表达式引擎regex表单验证无效
EN

Stack Overflow用户
提问于 2014-08-05 19:48:32
回答 1查看 491关注 0票数 1

有一个电子商务商店运行ExpressionEngine和Expresso商店。我正在努力进行适当的表单验证,但我一直遇到障碍。

Expresso Store文档中有这样的内容:

rules:field_name="rule"

除了使用require="field_name“参数外,更长的安全黑客样式语法也是可用的。对于每个字段,可以指定任何CodeIgniter表单验证规则。如果在require="field_name“参数之外使用此语法,则两者都适用。注意,order_email字段已经在valid_email规则中运行,您不需要手动指定它。

文档给出的示例代码:

代码语言:javascript
复制
rules:billing_name="required"
rules:billing_phone="numeric"
rules:billing_postcode="required|min_length[5]|max_length[5]"

这意味着可以指定普通的CodeIgniter表单字段规则-例如regex_match。但实际上,我不能让它起作用。也许我的Regex是错误的,但我并没有真正让任何“规则”(例如“数字”或“alpha”)可靠地工作。事实上,我只是将billing_name设置为数字(所以只有数字),并且输入了我的全名--绝对没有错误。

以前,我得到了一些错误--但只有当我使用alpha-数字时,它才会标记我正在使用的空格。但只适用于某些领域而不是其他领域。

本质上,我真正想做的就是剔除除空格以外的所有特殊字符。还有"-“。所以空间,句号和破折号。

也许我只是忽略了代码中的一些次要内容。这是我第一次尝试在Expresso中应用CodeIgniter表单验证规则。

您在下面看到的代码最终是我向regex中抛出任何我能做的事情的结果,只是为了得到一致的结果--错误或其他。

代码语言:javascript
复制
  {exp:store:checkout
    form_class="form-horizontal"
    next="/checkout/confirm"
    error_handling="inline"
    error_delimiters='<span class="checkoutError">|</span>'
    require="name|address1|city|country|postcode|order_email"
    rules:billing_address1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_address2="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:order_custom1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_name="regex_match[/^[0-9]{10}$/]"
    rules:billing_city="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_phone="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_postcode="numeric"

    rules:shipping_address1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_address2="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_name="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_city="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_phone="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_postcode="numeric"


    }

      {if no_items}
         {redirect="/checkout/"}
      {/if}

        <div class="w-row">
          <div class="w-col w-col-6 checkoutcolumn1">
            <div class="forminfotextblock">
              <p class="forminfotext">Contact Details</p>
            </div>
            <div class="formblock">
              <div class="w-form">
                  <div class="w-clearfix">
                    {error:billing_name}<label class="checkoutlabel" for="name">Name:</label>
                    <input class="w-input checkoutfield" id="billing_name" type="text" name="billing_name" value="{billing_name}">                   
                  </div>
                  <div class="w-clearfix">
                    {error:billing_address1}<label class="checkoutlabel" for="name-3">Address:</label>
                    <input class="w-input checkoutfield" id="billing_address1" type="text" name="billing_address1" value="{billing_address1}">
                  </div>
                  <div class="w-clearfix">
                     {error:billing_address2}<label class="checkoutlabel" for="name-4">Address 2:</label>
                    <input class="w-input checkoutfield" id="billing_address2" type="text" name="billing_address2" value="{billing_address2}">
                  </div>
                  <div class="w-clearfix">
                    {error:billing_city}<label class="checkoutlabel" for="name-5">City:</label>
                    <input class="w-input checkoutfield" id="billing_city" type="text" name="billing_city" value="{billing_city}">
                  </div>
                  <div class="w-clearfix">
                   {error:billing_postcode} <label class="checkoutlabel" for="name-6">Postcode:</label>
                    <input class="w-input checkoutfield" id="billing_postcode" type="number" name="billing_postcode" value="{billing_postcode}">
                  </div>
                  <div class="w-clearfix">
                    <label class="checkoutlabel" for="billing_state">State:</label>
                    <select class="w-select checkoutfield" id="billing_state" name="billing_state" value="{billing_state}">
                      {billing_state_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                   {error:billing_country}<label class="checkoutlabel" for="billing_country">Country:</label>
                    <select class="w-select checkoutfield" id="billing_country" name="billing_country" value="{billing_country}">
                      {billing_country_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                    {error:order_email}<label class="checkoutlabel" for="name-9">Email:</label>
                    <input class="w-input checkoutfield" id="order_email" type="text" name="order_email" value="{order_email}">
                  </div>
                  <div class="w-clearfix">
                   {error:billing_phone} <label class="checkoutlabel" for="name-10">Phone:</label>
                    <input class="w-input checkoutfield" id="billing_phone" type="text" name="billing_phone" value="{billing_phone}">
                  </div>
                  <div class="w-clearfix">
                     {error:order_custom1}<label class="checkoutlabel" for="name-11">Instructions:</label>
                    <input class="w-input checkoutfield" id="order_custom1" type="text" name="order_custom1" value="{order_custom1}">
                  </div>
                  <div class="w-checkbox">
                    <input type="hidden" name="shipping_same_as_billing" value="0" />
                    <input class="w-checkbox-input" id="shipping_same_as_billing" type="checkbox" name="shipping_same_as_billing" value="1" {shipping_same_as_billing_checked}>
                    <label class="w-form-label" for="shipping_same_as_billing">Same details for shipping</label>               
                  </div>
              </div>
            </div>
          </div>
          <div class="w-col w-col-6 checkoutcolumn2">
            <div class="forminfotextblock">
              <p class="forminfotext">Shipping Address</p>
            </div>
            <div class="formblock">
              <div class="w-form">
                  <div class="w-clearfix">
                    {error:shipping_name}<label class="checkoutlabel" for="name-14">Name:</label>
                    <input class="w-input checkoutfield" id="shipping_name" type="text" name="shipping_name" value="{shipping_name}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_address1}<label class="checkoutlabel" for="name-15">Address:</label>
                    <input class="w-input checkoutfield" id="shipping_address1" type="text" name="shipping_address1" value="{shipping_address1}">
                  </div>
                  <div class="w-clearfix">
                   {error:shipping_address2} <label class="checkoutlabel" for="name-16">Address 2:</label>
                    <input class="w-input checkoutfield" id="shipping_addres2" type="text" name="shipping_address2" value="{shipping_address2}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_city}<label class="checkoutlabel" for="name-17">City:</label>
                    <input class="w-input checkoutfield" id="shipping_city" type="text" name="shipping_city" value="{shipping_city}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_postcode}<label class="checkoutlabel" for="name-23">Postcode:</label>
                    <input class="w-input checkoutfield" id="shipping_postcode" type="number" name="shipping_postcode" value="{shipping_postcode}">
                  </div>
                  <div class="w-clearfix">
                    <label class="checkoutlabel" for="field-3">State:</label>
                    <select class="w-select checkoutfield" id="shipping_state" name="shipping_state" value="{shipping_state}">
                      {shipping_state_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_country}<label class="checkoutlabel" for="field-4">Country:</label>
                    <select class="w-select checkoutfield" id="shipping_country" name="shipping_country">
                      {shipping_country_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                   {error:shipping_phone} <label class="checkoutlabel" for="name-22">Phone:</label>
                    <input class="w-input checkoutfield" id="shipping_phone" type="text" name="shipping_phone" value="{shipping_phone}">
                  </div>
              </div>
            </div>
            <div class="forminfotextblock">
              <p class="forminfotext">Shipping Method</p>
            </div>
            <div class="formblock">
              <div class="w-form">
              {shipping_methods}
                  <div class="w-radio">
                   <label class="w-form-label" for="shipping_method">
                       <input type="radio" class="w-radio-input" name="shipping_method" value="{method_title}" id="{method_id}" />
                       {method_title}
                   </label>
                  </div>      
              {/shipping_methods}           


                 </div>
                 <span id="overnightInfo" style="display:none">
                  <input class="w-input overnightaccountinfo" id="field" type="text" placeholder="Enter your account number" name="field">
                  <br/>
                  <input class="w-input overnightaccountinfo" id="field-2" type="text" placeholder="Enter your account carrier" name="field-2" data-name="Field 2">
                 </span>
                 <br/>
                  <input class="button" name="next" type="submit" value="Review Order">
              </div>
            </div>
          </div>
        </div>
      <script> 
         //on click displays the account and carrier fields
         $('#2').click(function() {
            $("#overnightInfo").toggle(this.checked);
         });

         //on click hides the account and carrier fields
         $('#1').click(function() {
            $("#overnightInfo").toggle(this.hidden);
         });

         //copy billing information over to shipping information fields
         $(function(){
             $("#shipping_same_as_billing").change(function() {
                 if ($("#shipping_same_as_billing:checked").length > 0) {
                     bindGroups();
                 } else {
                     unbindGroups();
                 }
             });
         });

         var bindGroups = function() {
             // First copy values
             $("input[name='shipping_name']").val($("input[name='billing_name']").val());
             $("input[name='shipping_address1']").val($("input[name='billing_address1']").val());
             $("input[name='shipping_address2']").val($("input[name='billing_address2']").val());
             $("input[name='shipping_city']").val($("input[name='billing_city']").val());
             $("input[name='shipping_postcode']").val($("input[name='billing_postcode']").val());
             $("select[name='shipping_state']").val($("select[name='billing_state']").val());
             $("select[name='shipping_country']").val($("select[name='billing_country']").val());
             $("input[name='shipping_phone']").val($("input[name='billing_phone']").val());            

             // Then bind fields
             $("input[name='billing_name']").keyup(function() {
                 $("input[name='shipping_name']").val($(this).val());
             });
             $("input[name='billing_address1']").keyup(function() {
                 $("input[name='shipping_address1']").val($(this).val());
             });
             $("input[name='billing_address2']").keyup(function() {
                 $("input[name='shipping_address2']").val($(this).val());
             });
             $("input[name='billing_city']").keyup(function() {
                 $("input[name='shipping_city']").val($(this).val());
             });
             $("input[name='billing_postcode']").keyup(function() {
                 $("input[name='shipping_postcode']").val($(this).val());
             });
             $("select[name='billing_state']").keyup(function() {
                 $("select[name='shipping_state']").val($(this).val());
             });
             $("select[name='billing_country']").keyup(function() {
                 $("select[name='shipping_country']").val($(this).val());
             });
             $("input[name='billing_phone']").keyup(function() {
                 $("input[name='shipping_phone']").val($(this).val());
             });            
         };

         var unbindGroups = function() {
             $("input[name='billing_name']").unbind("keyup");
             $("input[name='billing_address1']").unbind("keyup");
             $("input[name='billing_address2']").unbind("keyup");
             $("input[name='billing_city']").unbind("keyup");
             $("input[name='billing_postcode']").unbind("keyup");
             $("select[name='billing_state']").unbind("keyup");
             $("select[name='billing_country']").unbind("keyup");
             $("input[name='billing_phone']").unbind("keyup");
         };          
      </script>

 {/exp:store:checkout}  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-12 15:08:39

弄明白了。本质上,CodeIgniter验证规则并不是以我所期望的方式触发的。某些字符正在被筛选,而另一些(')没有。所以,这是完全的用户错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25147097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档