在 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 中工作得很好。它显示一条消息:

"Please lengthen this text to 17 characters or more(You are currently use * characters)".

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

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

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

看看这个。

<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

尝试实现此代码段,希望此代码对您有所帮助

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