在WooCommerce产品搜索小部件中设置最小字符

时间:2016-01-13 09:13:22

标签: php html wordpress woocommerce woothemes

我正在使用WooCommerce产品搜索小部件,并在此代码中添加minlength="17"片段:

<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/'  ) ); ?>">
   <label class="screen-reader-text" for="s"><?php _e( 'Search for:', 'woocommerce' ); ?></label>
   <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search Products&hellip;', 'placeholder', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" minlength="17" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label', 'woocommerce' ); ?>" />
   <input type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" />
   <input type="hidden" name="post_type" value="product" />
</form>

它在Chrome中运行得非常好。它显示了一条消息:

  

“请将此文本延长至17个字符或更多(您目前是   使用*字符)“。

这是我需要的,但它不在 Firefox IE 中。 当我在搜索字段中放入3,4或少于17个字符时,它开始搜索。

怎么办?如何让它在所有浏览器中运行?!

3 个答案:

答案 0 :(得分:1)

你可以使用javascript代码

<input onblur="checkLength(this)" id="groupidtext" type="text" style="width: 100px;" minlength="6" />
<!-- or use event onkeyup instead if you want to check every character strike-->


function checkLength(el) {
  if (el.value.length <= 6) {
    alert("length must be atleast 6 characters")
  }
}

答案 1 :(得分:1)

检查一下。

<form action="">

<input minlength="17" >
<input pattern=".{17,}"   required title="17 characters minimum">
<input pattern=".{5,10}" required title="5 to 10 characters">

<input type="submit">
</form>

DEMO

答案 2 :(得分:1)

尝试实现此代码段,我希望此代码可以提供帮助

&#13;
&#13;
<link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">
 
<form id="myform">
<label for="field">Required, minimum length 17: </label>
<input type="text" class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      minlength: 17
    }
  }
});
</script>
&#13;
&#13;
&#13;

相关问题