关于HTML在input标签内检测输入内容合法性和在js验证表单合法性后跳转到servlet的几点内容

来源:互联网 发布:java ftp 文件排序 编辑:程序博客网 时间:2024/05/20 22:26

只能输入数字和字母

<input   onkeyup="value=value.replace(/[\W]/g,'') "

        onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"

        onkeydown="if(event.keyCode==13)event.keyCode=9"> 

只能输入数字

<input onkeyup="value=value.replace(/[^\d]/g,'') "
        onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,'')) " name="age">

只能输入汉字、数字、字母

<input onkeyup="value=value.replace(/[^\a-zA-Z\u4e00-\u9fa5]+$/g,'') "
        onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\a-zA-Z\u4e00-\u9fa5]+$/g,'')) ">


在js验证表单合法性后跳转到servlet


function checkedMobile() {  //检测手机号码
var sMobile = document.form.tel.value;
  if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))){  
        document.form.tel.focus(); 
        return false; 
   } 
   
    return true;
}
function checkEmail(){    //检测email格式
var email = document.form.email.value;
if(!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(email))){
document.form.email.focus();
return false;
}
return true;
}

function checkEmpty(){
var account = document.form.account.value;
var password = document.form.password1.value;
var rePassword = document.form.password2.value;
var name = document.form.name.value;
var sex = document.form.sex.value;
var age = document.form.age.value;
var school = document.form.school.value;
var tel = document.form.tel.value;
var email = document.form.email.value;
var operator = document.form.operator.value;

if(account == "" || password == "" || rePassword == "" || name == "" || sex == "" || age == ""
|| school == "" || tel == "" || email == "" || operator == ""){
alert("请您填写所需要的信息"); 
}else if(password.length < 6 || rePassword.length < 6){
alert("密码长度不少于6位");
}else if(password != rePassword){
alert("您两次输入的密码不相同");
}else if(!checkedMobile()){
alert("电话号码格式不正确");
}else if(!checkEmail()){
alert("电子邮件格式不对");
}else {
//跳转到servlet页面
document.form.action = "AddStuAndTeaServlet"; 
document.merchantForm.submit(); 
}
}

0 0