<script language="javascript">
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("请输入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>
页面控件为input的
运行报错:无法获取属性:“name”的值:对象为null或未定义
<!--
function CheckForm()
{
if (document.form.name.value.length == 0) {
alert("请输入您姓名!");
document.form.name.focus();
return false;
}
return true;
}
-->
</script>
页面控件为input的
运行报错:无法获取属性:“name”的值:对象为null或未定义
{ if (document.forms[0].name.value.length == 0) {
alert("请输入您姓名!");
document.forms[0].name.focus();
return false;
}
return true;
}
<input id="name" />
</form>
<!--
function CheckForm()
{
var put = document.getElementById("put");
if (put.value==null || put.value=="") {
alert("请输入您姓名!");
put.focus();
return false;
}
return true;
}
-->
</script>
<body>
<input type="text" id="put" />
</body>验证表单中的radio和check控件 check控件的name值设置为相同的值 然后根据document.getElementsByName得到一个数组 再遍历这个数组 检查是否处于选中状态这个是验证check控件至少选择其中一个的验证:
function validate(){
var publishOrreceive = document.getElementsByName("publishOrreceive");
var b = false;
for(i=0;i<publishOrreceive.length;i++){
if(publishOrreceive[i].checked == true){
b = true;
break;
}
}
if(!b){
alert('保存、发布或接收至少选择一个');
}
return b;
}
这是对应的复选框
<input type="checkbox" id="saveData" name="publishOrreceive" onclick="PubOrRecDataShow()" value="save"/>
<label onclick="PubOrRecDataShow()" for="saveData">保存</label>
<input type="checkbox" id="publishData" name="publishOrreceive" onclick="PubOrRecDataShow()" value="publish"/>
<label onclick="PubOrRecDataShow()" for="publishData">发布</label>
<input type="checkbox" id="receiveData" name="publishOrreceive" onclick="PubOrRecDataShow()" value="receive"/>
<label onclick="PubOrRecDataShow()" for="receiveData">接收</label>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
function showAge(){
var age = document.frm.txtAge.value; //设置ID,用document.getElementById更好..
alert(age);
}
</script>
</head><body>
<form name='frm'>
<input name='txtAge' value='11' type='text'>
<input type='button' value='showAge' onclick='showAge()' />
</body></html>
function CheckForm()
{
var put = document.getElementById("put");
if (put.value==null || put.value=="") {
alert("请输入您姓名!");
put.focus();
return false;
}
return true;
}
html:<input type="text" name=name id="name" /><script language="javascript">
function checkform(){
var int_name=document.getElementById("name");
if(int_name.value.length==0)
{
alert("不能为空~")
int_name.focus();
return false;
}else{
return true;
}
}
</script>2.jquery方法:
<input type="text" name="name" class="name" id="user" />
$(function(){
$("input.name").blur(function(){
if($(this).val().length==0){
alert("不能为空");
$(this).focus();
return false;
}else{ return true;}
});
});