[英]Java script Validation that one of f:selectItem must be selected
I want to write a javascript validation code for validating that one of f:selectItem must be selected but i'm unable to access checked property of f:selectItem through java script. 我想编写一个JavaScript验证代码来验证必须选择f:selectItem之一,但是我无法通过Java脚本访问f:selectItem的选中属性。 Below is my JSF page. 以下是我的JSF页面。
JSF Page:- JSF页面:-
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<f:view>
<body
<h:form styleClass="form" id="form1"">
<h:selectOneRadio id="searchType" value="#{auditTrailDTO.searchType}">
<f:selectItem itemLabel="Male" itemValue="m"/>
<f:selectItem itemLabel="FeMale" itemValue="f"/>
</h:selectOneRadio>
</h:form>
</body>
</f:view>
</html>
AuditTrailDTO.java:- AuditTrailDTO.java:-
class AuditTrailDTO
private string searchType;
public String getSearchType() {
return searchType;
}
public void setSearchType(String searchType) {
this.searchType = searchType;
}
}
Edit:- 编辑:-
I have succeeded in accessing f:selectItem by following way.Now when no one is selected it shows alert message as expected.But it shows alert message even when second of the radio button is selected. 我已经通过以下方式成功访问了f:selectItem。现在当没有人被选中时,它会按预期显示警报消息。但是即使选择了第二个单选按钮,它也会显示警报消息。 I'm calling below method on form onsubmit attribut.Any idea please? 我在表单onsubmit属性上调用下面的方法。
function validateForm(){
var searchType = document.getElementsByName("form1:searchType");
var a = !(searchType[0].checked);
var b = !(searchType[1].checked);
if(a&&b){
alert('Please select search type');
return false;
}
return true;
}
Set the required attribute on the tag 在标签上设置必需的属性
If the rendering is 如果渲染为
<input type="radio" name="form1:searchType" value="Male">
<input type="radio" name="form1:searchType" value="FeMale">
then you can do this in plain JS DEMO 那么你可以在普通的JS DEMO中做到这一点
window.onload=function() {
document.getElementById('form1').onsubmit=function() {
var isChecked = this.elements["form1:searchType"][0].checked || this.elements["form1:searchType"][1].checked;
if (!isChecked) {
alert('Please check gender');
return false
}
}
}
You can try with required
and requiredMessage
attributes: 您可以尝试使用required
和requiredMessage
属性:
<h:selectOneRadio required="true" requiredMessage="Message... ">
...
</h:selectOneRadio>
You can catch the requiredMessage
with: 您可以使用以下命令捕获requiredMessage
:
<h:messages>
or <h:message>
<h:messages>
或<h:message>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.