[英]Validation for radio buttons and drop down
how do I create a validation for my radio buttons and drop down box? 如何为单选按钮和下拉框创建验证? Do I need to create a
<script>
function on top? 我是否需要在顶部创建一个
<script>
函数?
<tr>
<td class="alt"><label for="period"><b>Transaction Period:</b> </label></td>
<td><input type="radio" name="period" value="current">Current Month<br>
<input type="radio" name="period" value="current_first">Last 1 Month and Current Month<br>
<input type="radio" name="period" value="current_second">Last 2 Months and Current Month</td>
</tr>
<td class="alt"><label for="type"><b>Type: </b></label></td>
<td>
<input type="radio" name="type" value="both">Credit and Debit<br>
<input type="radio" name="type" value="credit">Credit Only<br>
<input type="radio" name="type" value="debit">Debit Only
</td>
<tr>
<td class="alt"><label for="sort"><b>Sort According To: </b></label></td>
<td> <select name="sort">
<option value="latest">Latest Transaction First</option>
<option value="earliest">Earliest Transaction First</option>
<option value="codes">Transaction Codes</option>
</select>
</td>
</tr>
To highlight invalid inputs, use the :invalid
pseudo-class. 要突出显示无效输入,请使用
:invalid
伪类。 This won't work in older browsers , but newer browsers do all support using it on inputs. 这在较旧的浏览器中不起作用,但是较新的浏览器都支持在输入中使用它。 For the inputs, you will have to add the
required
attribute, and you'll need to add a tag around their label (for example a <span>
) for this to work though: 对于输入,您将必须添加
required
属性,并且需要在其标签周围添加标签(例如<span>
),以使该标签有效:
New HTML code: 新的HTML代码:
<table>
<tr>
<td class="alt">
<label for="period"><b>Transaction Period:</b>
</label>
</td>
<td>
<input type="radio" required name="period" value="current"><span>Current Month</span>
<br>
<input type="radio" required name="period" value="current_first"><span>Last 1 Month and Current Month</span>
<br>
<input type="radio" required name="period" value="current_second"><span>Last 2 Months and Current Month</span>
</td>
</tr>
<td class="alt">
<label for="type"><b>Type: </b>
</label>
</td>
<td>
<input type="radio" required name="type" value="both"><span>Credit and Debit</span>
<br>
<input type="radio" required name="type" value="credit"><span>Credit Only</span>
<br>
<input type="radio" required name="type" value="debit"><span>Debit Only </span>
</td>
<tr>
<td class="alt">
<label for="sort"><b>Sort According To: </b>
</label>
</td>
<td>
<select name="sort" required>
<option value="" selected disabled>Please select an option</option>
<option value="latest">Latest Transaction First</option>
<option value="earliest">Earliest Transaction First</option>
<option value="codes">Transaction Codes</option>
</select>
</td>
</tr>
</table>
CSS: CSS:
input:invalid + span {
background:#F99;
}
select:invalid {
color:#F55;
}
This :invalid
pseudo-class will select an input element if it: 如果
:invalid
此:invalid
伪类将选择一个输入元素:
type="number"
, and the entered input does not meet that specific input type's value type="number"
,并且输入的输入不符合该特定输入类型的值 <select>
having an empty-valued option selected, or radio inputs not having one of the radio buttons it shares its name with selected <select>
了一个空值选项,或者单选按钮没有单选按钮之一,它与选择的名称共享其单选按钮 pattern
or max
defined, and the input does not match the entered pattern, or exceeds the maximum value. pattern
或max
类的属性,并且输入与输入的样式不匹配或超过最大值。 If any of the inputs is still :invalid
, the form won't be submitted, even when pressing the submit button. 如果任何输入仍然
:invalid
,即使按下提交按钮,也不会提交表单。 ( simple example ) ( 简单的例子 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.