javascript怎样做数据验证

来源:百度知道 编辑:UC知道 时间:2024/06/02 03:13:32
想做一个对客户保存资料时,用户名(只能输入数字或字母),密码(只能填写数字),EMAIL(只能输入符合电子邮件格式的字符)的验证判断
<script type="text/javascript">
function my_submit()
{
if(!(event.keyCode>32&&event.keyCode<48)|| (event.keyCode>57&&event.keyCode<65)||(event.keyCode>90&&event.keyCode<97)||(event.keyCode>122&&event.keyCode<127))
{alert("只能输入数字或字母");return ;}
if((document.formadd.usermima.value!=/^[0-9]$/)||(document.formadd.usermima.value!=/^[a-zA-Z]$/))
{alert("只能输入数字或字母");
form.usermima.focus();
return;
}
if(document.formadd.postalcode.value!=/^[0-9]$/)
{alert("只能填写数字");
form.usermima.focus();
return;
}
if((document.formadd.email.value(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!=-1))
{
alert("Email无效!");
return;

<html>
<head>
<script>
function check(){
var username=document.form1.username.value;
var password=document.form1.password.value;
var email=document.form1.email.value;
if(!username.match(/^[0-9a-zA-Z]+$/)){
alert("只能输入数字或字母");
}else if(!password.match(/^\d+$/)){
alert("只能输入数字");
}else if(!email.match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+$/)){
alert("邮件格式错误");
}
}
</script>
</head>
<body>
<form name="form1">
用户名:<input name="username"><br>
密码:<input type="password" name="password"><br>
E-mail:<input name="email"><br>
<input type="button" value="提交" onclick="check()">
</form>
</body>
</html>

判断是不是数字字母下划线,用