function createCode()
{ //创建验证码函数
code = ""
var codeLength =5//验证码的长度
var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')//所有候选组成验证码的字符,当然也可以用中文的
for(var i=0i<codeLengthi++)
{
var charIndex =Math.floor(Math.random()*36)
code +=selectChar[charIndex]
}// 设置验证码的显示样式,并显示
document.getElementById("discode").style.fontFamily="Fixedsys" //设置字体
document.getElementById("discode").style.letterSpacing="3px" //字体间距
document.getElementById("discode").style.color="#ff0000" //字体颜色
document.getElementById("discode").innerHTML=code // 显示
}</script>把上面一段JS代码放到<head></head>标签中在页面装载的时候,调用验证码创建函数 <body onload="createCode()
">在验证码输入框的后面放一个标签 <span id="discode"></span>
这个用插件来验证的比较快。插件名称:formvalidator.js ,网上下载个,使用方法:$("#new_password").formValidator({empty:true,onshow:"不修改密码请留空。",onfocus:"密码应该为6-20位之间"}).inputValidator({min:5,max:20,onerror:"密码应该为6-20位之间"})$("#new_pwdconfirm").formValidator({empty:true,onshow:"不修改密码请留空。",onfocus:"请输入两次密码不同。",oncorrect:"密码输入一致"}).compareValidator({desid:"new_password",operateor:"=",onerror:"请输入两次密码不同。"})
页面就不写了,帮你把验证的js方法写好了。 调用的话,你在form的onsubmit时调用就行。
<script>//用户名要以字母开头;
var isUserName = function(s){
var patrn=/^[a-zA-Z]+$/
if (!patrn.exec(s))
return false
return true
}
//两次输入的密码需要一样;
var isPassword = function(s1,s2){
if(!s1==s2)
return false
return true
}
//输入的EMAIL地址中必须要有’@’
var isEmail = function(s){
var patrn=/^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$/
if (!patrn.exec(s))
return false
return true
}
//有些输入项是必填项,不能为空;
var isEmpty = function(s){
var patrn=/^[\s\S]+$/
if (!patrn.exec(s))
return false
return true
}
</script>
希望对你有用。