1、javascript代码
//控制输入项目不为空, 控件需设置 nonull 、elementname 属性
function CheckNull() {
//debugger;
var objs = document.getElementsByTagName("input");
if (!CheckNullByObjs(objs)) {
return false;
}
var objs2 = document.getElementsByTagName("select");
if (!CheckNullByObjs(objs2)) {
return false;
}
var objs3 = document.getElementsByTagName("textarea");
if (!CheckNullByObjs(objs3)) {
return false;
}
return true;
}
//去掉前后空格
function Trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function CheckNullByObjs(objs) {
for (var i = 0; i < objs.length; i++) {
if (objs[i].attributes["nonull"] != undefined && objs[i].attributes["nonull"].value == "true" && Trim(objs[i].value).toString() == "") {
alert(objs[i].attributes["elementname"].value + "不能为空");
objs[i].focus();
return false;
}
}
return true;
}
2、文本框属性设置代码这两个属性必须设置nonull=”true” elementname=”姓名” ,nonull=”false” 表示可空,nonull=”true” 表示不可空,可以不设置相应nonull=”true” 的属性。
<asp:TextBox type="text" nonull="true" elementname="姓名" name="txtPERSONNAME" Height="20px"
Width="150px" ID="txtPERSONNAME" runat="server" onkeydown="return txtonkeydown(event);"></asp:TextBox>
本文介绍了一种使用JavaScript进行前端表单验证的方法,包括检查输入项是否为空及自定义提示信息等功能。通过设置特定属性实现对不同类型的表单元素进行有效性验证。
755

被折叠的 条评论
为什么被折叠?



