在Drp中添加按钮的时候要验证用户输入的是否合法利用JavaScript就可以实现这个功能下面就是我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="client_validate.js"></script>
<script type="text/javascript">
function goBack() {
window.self.location="user_maint.html"
} function addUser() {
var vUserId=document.getElementById("userId");
var vUserName=document.getElementById("userName");
var vPassword=document.getElementById("password");
var vContactTel=document.getElementById("contactTel");
var vEmail=document.getElementById("email"); /**用户代码验证**/
//用户代码不能为空
if(trim(vUserId.value)==""){
alert("用户代码不能为空!");
vUserId.focus();
return;
}
//判断字符长度大于4
if(trim(vUserId.value).length<4){
alert("用户代码长度不能小于4!");
vUserId.focus();
vUserId.select();
return;
} //用户代码只能是数字和字母
var re=new RegExp(/^[0-9a-zA-Z]{4,6}$/);
if(!re.test(trim(vUserId.value)))
{
alert("用户代码必须是字母或数字,只能为4~6位");
vUserId.focus();
vUserId.select();
return ;
} //第一个字符为字母
re.compile(/^[a-zA-Z]/);
if(!re.test(trim(vUserId.value)))
{
alert("用户代码首字符必须是字母");
vUserId.focus();
vUserId.select();
return ;
} /**用户名称验证**/
//用户名必须输入
if(trim(vUserName.value)==""){
alert("用户名称不能为空!");
vUserName.focus();
vUserName.select();
return;
} //用户名不能和用户代码一样
if(trim(vUserName.value)==trim(vUserId.value)){
alert("用户名不能和用户代码一样!");
vUserName.focus();
vUserName.select();
return;
} /**密码验证**/
//判断密码字符长度大于6
if(trim(vPassword.value).length<6){
alert("密码长度不能小于6!");
vPassword.focus();
vPassword.select();
return;
} /**联系电话验证**/
//如果联系电话不为空则验证联系电话为数字
if (trim(vContactTel.value) != "") {
//采用正则
re.compile(/^[0-9]+$/);
if (!re.test(vContactTel.value)) {
alert("电话号码不合法!");
vContactTel.focus();
return;
}
} /**Email验证**/
if(trim(vEmail.value)!=""){
re.compile(/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/);
if(!re.test(trim(vEmail.value))){
alert("Email不合法!");
vEmail.focus();
vEmail.select();
return;
}
} /* document.getElementById("userForm").action="user_add.jsp";
document.getElementById("userForm").method="post";
document.getElementById("userForm").submit(); */
with (document.getElementById("userForm")){
action="user_add.jsp";
method="post";
submit();
} } function linit(){
document.getElementById("userId").focus();
}
/*禁止输入除了字母以外的东西*/
function userIdOnKeyPress(){
if(!(event.keyCode>=97 && event.keyCode<=122)){
event.keyCode=0;
}
}
/*按回车键变为Tab键,如果到了添加按钮则提交数据*/
function document.onkeydown() {
if(window.event.keyCode==13 && window.event.srcElement.type!='button'){
window.event.keyCode=9;
}
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head> <body class="body1" onload="linit()">
<form name="userForm" target="_self" id="userForm">
<div align="center">
<table width="95%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td>  </td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="0"
height="25">
<tr>
<td width="522" class="p1" height="25" nowrap>
<img src="../images/mark_arrow_03.gif" width="14" height="14">
 
<b>系统管理>>用户维护>>添加</b>
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<table width="95%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="22%" height="29">
<div align="right">
<font color="#FF0000">*</font>用户代码: 
</div>
</td>
<td width="78%">
<input name="userId" type="text" class="text1" id="userId"
size="10" maxlength="10" onkeypress="userIdOnKeyPress();">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>用户名称: 
</div>
</td>
<td>
<input name="userName" type="text" class="text1" id="userName"
size="20" maxlength="20">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
<font color="#FF0000">*</font>密码: 
</div>
</td>
<td>
<label>
<input name="password" type="password" class="text1"
id="password" size="20" maxlength="20">
</label>
</td>
</tr>
<tr>
<td height="26">
<div align="right">
联系电话: 
</div>
</td>
<td>
<input name="contactTel" type="text" class="text1"
id="contactTel" size="20" maxlength="20">
</td>
</tr>
<tr>
<td height="26">
<div align="right">
email: 
</div>
</td>
<td>
<input name="email" type="text" class="text1" id="email"
size="20" maxlength="20">
</td>
</tr>
</table>
<hr width="97%" align="center" size=0>
<div align="center">
<input name="btnAdd" class="button1" type="button" id="btnAdd"
value="添加" onClick="addUser()">
    
<input name="btnBack" class="button1" type="button" id="btnBack"
value="返回" onClick="goBack()" />
</div>
</div>
</form>
</body>
</html>

需要下载的Js

DRP中用到的JavaScript验证的更多相关文章

  1. Eclipse去除JavaScript验证错误

    这篇文章主要是对Eclipse去除js(JavaScript)验证错误进行了介绍.在Eclipse中,js文件常常会报错.可以通过如下几个步骤解决 第一步: 去除eclipse的JS验证: 将wind ...

  2. JavaScript验证正则表达式大全

    JavaScript验证正则表达式大全,搜集最全的JavaScript验证正则表达式,开始查看吧,这里的都是正则表达式的例子,具体和函数结合的使用方法,还请查看下篇文章<JavaScript使用 ...

  3. 【转】Eclipse去除js(JavaScript)验证错误

    这篇文章主要是对Eclipse去除js(JavaScript)验证错误进行了介绍.在Eclipse中,js文件常常会报错.可以通过如下几个步骤解决 第一步:去除eclipse的JS验证:将window ...

  4. javascript 验证 yyyy-MM-dd HH:mm:ss 的正则表达式

    原文:javascript 验证 yyyy-MM-dd HH:mm:ss 的正则表达式 ^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13 ...

  5. 使用JavaScript验证用户输入的是否为正整数

    在项目开发中,需要使用JavaScript验证用户输入的是否为正整数. 方法一: var type="^[0-9]*[1-9][0-9]*$"; var r=new RegExp( ...

  6. 纯javascript验证,100行超精简代码。

    这篇文章转自--寒飞,原帖地址http://blog.csdn.net/luoyehanfei/article/details/42262249 QQ交流群235032949 纯javascript验 ...

  7. 转发 eclipse 取消javascript 验证

    博客地址: http://blog.csdn.net/itchiang/article/details/7498474 最近出了一个很怪的现象,某一个js文件,在某一个Eclipse工程中呆的好好的, ...

  8. JavaScript验证函数大全

    1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...

  9. 常用的JavaScript验证正则表达式1

    匹配Email地址的正则表达式:w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*评注:表单验证时很实用 匹配网址URL的正则表达式:[a-zA-z]+://[^s]* 评注:网 ...

随机推荐

  1. iOS之GCD的局部解析

    一什么是GCD :(Grand [伟大] Central [中央]  Dispatch[调度]) GCD又名“伟大的中央调度器”,他是iOS4后才引进的一种多线程技术.开发者只需定义想执行的任务兵追加 ...

  2. html 中根据后台参数显示 相应的样式 EL表达式

    <li><a class="${return_product_statu==-1||return_product_statu==null?'switch_xz':'none ...

  3. chrom浏览器避免弹出“确定要离开此面吗?”提示框

    一.避免弹出提示框 在网上搜了很多,答案大都是设置window.onbeforeunload=null ,但是试用之后无效. 这个问题放了两天之后返回来再次想,终于找到了答案,在此和大家分享一下: 解 ...

  4. web 分类 和使用Dreamweaver

    1.iE 2.火狐的fox 3.谷歌的Chrome 在编写的代码要符合W3c统一的国际标准 二:使用Dreamweaver: Dreamweaver只能用辅助,生成的代码要修改,…… ———编写的代码 ...

  5. ASP.NET后台注册JS的方法

    1. 用Response.Write方法 代码如下: Response.Write("<script type='text/javascript'>alert("hel ...

  6. php 笔试面试 总结

    一次小小的笔试面试经历,虽然是一些简单的问题,但是自己在这儿总结一下,也查一些资料,得出一些较好的答案,也能帮助自己成长. 1.自己熟悉的http状态码及其意义 其实这个题答案随处可见.这儿也还是记录 ...

  7. jQuery图片提示和文字提示

    图片提示: 效果如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  8. Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed

    最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...

  9. Sharepoint 2013 系列篇(安装部署)--上篇

    前言 sharepoint的部署是按照物理拓扑图的架构来部署,按照物理拓扑图架构分为一层拓扑图架构,二层拓扑图架构,三层拓扑图架构,多层拓扑图架构. 按照分层的拓扑图部署是按照需求来划分的,一层拓扑图 ...

  10. dom操作中的js优化

    频繁地对于DOM进行操作的很是损耗性能,但在富网页应用中我们编写脚本无可避免地要跟DOM打交道,到底怎么才能优化这个性能瓶颈呢,大致从以下三种情况去考虑: 访问和修改DOM元素 修改DOM样式,会造成 ...