学习正则表达式,让编程更方便
[color=red]输入框中只能输入1~6位数字[/color]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test1(){
var s = document.getElementById("name").value;
var patrn=/^[0-9]{1,6}$/;
if (patrn.exec(s) == null) alert('false');
else
alert('true');
}
</script>
</head>
<body>
<form action="" method="post">
<input id="name" name="username" οnchange="test1()"/>
</form>
</body>
</html>
判断是否为汉字
<script>
alert(/^[\u4E00-\u9FA5]$/.test("啰"));1
alert(/^[^\x00-\xff]$/.test("沒")); 2
</script>
[color=red]输入框中只能输入1~6位数字[/color]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>test.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test1(){
var s = document.getElementById("name").value;
var patrn=/^[0-9]{1,6}$/;
if (patrn.exec(s) == null) alert('false');
else
alert('true');
}
</script>
</head>
<body>
<form action="" method="post">
<input id="name" name="username" οnchange="test1()"/>
</form>
</body>
</html>
判断是否为汉字
<script>
alert(/^[\u4E00-\u9FA5]$/.test("啰"));1
alert(/^[^\x00-\xff]$/.test("沒")); 2
</script>