除非文本字段超过2个字符,否则不要提交表单

Bra*_*her -1 javascript php wordpress

我有这个简单的表格用于我的网站搜索(Wordpress)

<form action="/" method="get">
    <label for="search">Search</label>
    <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
    <input type="image" class="search-btn" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/img/magnifier13.png" width="16" height="16" />
</form>
Run Code Online (Sandbox Code Playgroud)

我不希望人们在搜索字段中不少于2个字符的情况下搜索任何内容,我该如何进行此操作?最好用JS.

Ank*_*iiG 7

您可以使用patternHTML5 的属性.该required属性也是必需的,否则将从约束验证中排除具有空值的输入字段.

<input pattern=".{2,}"   required title="2 characters minimum">
<input pattern=".{3,10}" required title="3 to 10 characters">
Run Code Online (Sandbox Code Playgroud)