javascript 验证小结
1,验证文本框中的值是不是数字,并且设置数字大小范围

2,长度限制

3,非法值校验(设定不能输入的值)

4,中文/英文/数字/邮件地址合法性判断

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="baidu.aspx.cs" Inherits="WebApplication1.baidu" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function isEnglish(name) //英文值检测
{
alert(name);
if (name.length == 0) {
return false;
}
for (i = 0; i < name.length; i++) {
// if ((name.charCodeAt(i) > 'A' && name.charCodeAt(i) < 'Z') || (name.charCodeAt(i) > 'a' && name.charCodeAt(i) < 'z'))
if (isNaN(name)) {
return true;
}
return false;
}
}
function isChinese(name) //中文值检测
{
if (name.length == 0)
return false;
for (i = 0; i < name.length; i++) {
if (name.charCodeAt(i) > 128) {
return true;
}
}
return false;
}
function isMail(name) // E-mail值检测
{
if (!isEnglish(name)) {
return false;
i = name.indexOf("@");
j = name.lastIndexOf("@");
if (i == -1) {
return false;
}
if (i != j) {
return false;
}
if (i == name.length) {
return false;
}
}
return true;
}
function isNumber(name) //数值检测
{
if (name.length == 0)
return false;
for (i = 0; i < name.length; i++) {
if (name.charAt(i) < "0" || name.charAt(i) > "9")
return false;
}
return true;
}
function CheckForm() {
if (!isMail(document.getElementById("Text1").value)) {
alert("您的电子邮件不合法!");
document.getElementById("Text1").focus();
return false;
}
if (!isEnglish(document.getElementById("Text2").value)) {
alert("英文名不合法!");
document.getElementById("Text2").focus();
return false;
}
if (!isChinese(document.getElementById("Text3").value)) {
alert("中文名不合法!");
document.getElementById("Text3").focus();
return false;
}
if (!isNumber(document.getElementById("Text4").value)) {
alert("邮政编码不合法!");
document.getElementById("Text4").focus();
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
电子邮件:
<input id="Text1" type="text" /></br> 英文名:
<input id="Text2" type="text" /></br> 中文名
<input id="Text3" type="text" /></br> 邮政编码
<input id="Text4" type="text" />
<input id="Button1" type="button" onclick="CheckForm()" value="button" />
</div>
</form>
</body>
</html>
5,表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等

6,判断是否全是数字

7,判断字符和汉字

8,判断是否是Email地址

9,数字格式校验

10,电话号码校验

<script language="javascript">
function fucCheckTEL(TEL) {
var i, j, strTemp;
strTemp = "0123456789-()# ";
for (i = 0; i < TEL.length; i++) {
j = strTemp.indexOf(TEL.charAt(i));
if (j == -1) {
//说明有字符不合法
alert("电话号码不合法");
return false;
}
}
//说明合法
alert("电话号码合法");
return 1;
}
function CheckForm() {
var o = document.getElementById("Text1").value;
fucCheckTEL(o);
}
</script>
//在文本框失去焦点是触发
<input id="Text1" type="text" onblur="CheckForm()" />
11,判断是否是中文

12,判断用户名是否含有数字字母下划线

13,判断两次输入是否一致

<script language="javascript">
function issame(str1, str2) {
if (str1 == str2) {
alert("相等");
return true;
}
else {
alert("不相等");
return false;
}
}
function CheckForm() {
var o = document.getElementById("Text1").value;
var o1 = document.getElementById("Text2").value;
issame(o, o1);
}
</script>
<input id="Text1" type="text" />
<input id="Text2" type="text" />
<input id="Button1" type="button" onclick="CheckForm()" value="button" />