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


Set minimum characters in WooCommerce product search widget

我正在使用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个字符或更多(您当前使用*字符)".

这是我需要的,但在FirefoxIE中没有。当我在搜索字段中放入3,4或更少的17个字符时,它开始搜索。

该怎么办?如何使它在所有浏览器中都能工作?!

看看这个。

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

演示

您可以使用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")
  }
}

尝试实现这个片段,我希望这段代码能帮助

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