JS表单验证姓名不能为空且一定为中文

JavaScript020

JS表单验证姓名不能为空且一定为中文,第1张

就这么2句代码就写得乱七八糟的

function checkName(){

if(inputname.value.length!=0){if(value.match(/^[\u4e00-\u9fa5]+$/))alert('ok')else alert('enter chinese')}else{alert('not empty')}}

<%@ page language="java" contentType="text/htmlcharset=utf-8"

pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8">

<title>Insert title here</title>

<script type="text/javascript">

function submit(){

var b1 = valiDateAge()

var b2 = valiDateName()

var b3 = valiDatePhone()

if(!(b1 &&b2 &&b3)){}

alert("输入有误!")

return false

}else{

return true

}

function valiDateAge(){

var age = document.getElementById('age').value

var ageReg = "/^[0-9]$/g"

var flag = false

if(ageReg.test(age)&&age<=200){

flag = true

}

return flag

}

function valiDateName(){

var name = document.getElementById('name').value

var nameReg= "/^[\u4e00-\u9fa5]{1,3}$/g"

var flag= false

if(nameReg.test(name)){

nameage = true

}

return flag

}

function valiDatePhone(){

var phone= document.getElementById('phone').value

var phoneReg = "^[/d]{11}$"

var flag = false

if(phoneReg.test(phone)){

flag = true

}

return flag

}

</script>

</head>

<body>

<form action="" onsubmit="return submit()">

<table>

<tr>

<td>名字:</td>

<td><input type="text" id="name"/></td>

</tr>

<tr>

<td>年龄: </td>

<td><input type="text" id="age"/></td>

</tr>

<tr>

<td>

电话:

</td>

<td><input type="text" id="phone"/></td>

</tr>

<tr>

<td><input type="submit" value="提交"/></td>

</tr>

</table>

</form>

</body>

</html>

没测试 环境原因 不知道正确不正确