js简单表单验证

//验证用户名

function checkName(){
var name = document.getElementById("name").value;
if(name == null || name == ""){
alert("用户名不能为空");
return false;
}else{
return true;
}
}

//验证密码

function checkPassword1(){
var password = document.getElementById("password1").value;
if(password == null || password == ""){
alert("密码不能为空");
return false;
}else if(password.length < 6 || password.length > 16){
alert("密码必须在6到16个字符内");
return false;
}else{
return true;
}
}

//验证密码是否相等

function checkPassword2(){
var password1 = document.getElementById("password1").value;
var password2 = document.getElementById("password2").value;
if(password2 == null || password2 == ""){
alert("不能为空");
return false;
}else if(password1 != password2){
alert("两次输入密码不一致");
return false;
}else{
return true;
}
}

//判断电话号码
//number:要判断的文本框,resultStr:返回的消息存放处

function checkNumber(number,returnStr){
var num = document.getElementById(number).value;
var re = /^[0-9,]*$/; //验证只能为数字的正则表达式
if(!re.test(num)){
document.getElementById(returnStr).innerHTML="<font color='red'>电话只能输入数字</font>";
document.getElementById(number).focus(); //设置文本框得到光标
return false;
}else if(num.length < 8 || num.length > 12){//如果字符的长度小于0,或者大于11
document.getElementById(returnStr).innerHTML="<font color='red'>电话不能为空,并且不能超过8-12位字符</font>";
document.getElementById(number).focus(); //设置文本框得到光标
return false;
}else {
document.getElementById(returnStr).innerHTML="<font color='green'>填写合格</font>";
return true;
}
}

//验证邮箱

function checkEmail(e_mail,returnStr){
var email = document.getElementById(e_mail).value;
var str = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
if(!str.test(email)){
document.getElementById(returnStr).innerHTML="<font color='red'>邮箱格式不正确,格式如:xxx@xx.com</font>";
return false;
}else{
document.getElementById(returnStr).innerHTML="<font color='green'>填写合格</font>";
return true;
}
}

//表单验证

function formCheck(){
if(checkName() && checkPassword1() && checkPassword2() && checkAge() &&checkEmail()){
return true;
}else{
return true;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值