javascript 验证小结

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

View Code
function fx() {
var o
= document.getElementById( " txt_finsh " ).value;
if (isNaN(o)) {
alert(
" 请输入如数字 " );
document.getElementById(
" txt_finsh " ).focus()
}
else {
if (parseInt(o) > 100 || parseInt(o) < 0 ) {
alert(
" 大于100或者小于0 " );
document.getElementById(
" txt_finsh " ).focus()
}

}
}
< asp:TextBox ID = " txt_finsh " runat = " server " onblur = " fx() " Width = " 133px " ></ asp:TextBox >

2,长度限制

View Code
< script >
function test()
{
if (document.a.b.value.length > 50 )
{
alert(
" 不能超过50个字符! " );
document.a.b.focus();
return false ;
}
}
</ script >
< form name = a onsubmit = " return test() " >
< textarea name = " b " cols = " 40 " wrap = " VIRTUAL " rows = " 6 " ></ textarea >
< input type = " submit " name = " Submit " value = " check " >
</ form >

// 或者
< script type = " text/javascript " >
function test()
{

if (document.getElementById( " b " ).value.length > 50 )
{
alert(
" 不能超过50个字符! " );
document.getElementById(
" b " ).focus();
return false ;
}
}
</ script >
< textarea id = " b " cols = " 40 " wrap = " VIRTUAL " rows = " 6 " ></ textarea >
< input id = " Button1 " type = " button " onclick = " test() " value = " button " />
</ form >

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

View Code
< script language = " javascript " >
<!--

function contain(str,charset)
// 字符串包含测试函数
{
var i;
for (i = 0 ;i < charset.length;i ++ )
if (str.indexOf(charset.charAt(i)) >= 0 )
return true ;
return false ;
}

function CheckForm()
{
if ((contain(document.getElementById( " Text1 " ).value, " %\(\)>< " ))

|| (contain(document.getElementById( " Text1 " ).value, " %\(\)>< " )))
{
alert(
" 输入了非法字符 " );
document.getElementById(
" Text1 " );
return false ;
}
return true ;
}
// -->
</ script >
< input id = " Text1 " type = " text " />
< input id = " Button2 " type = " button " onclick = " CheckForm() " value = " button "

/>

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

View Code
<% @ 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,表单项只能为数字和"_",用于电话/银行帐号验证上,可扩展到域名注册等

View Code
< script language = " javascript " >

function isNumber(String) {
var Letters
= " 1234567890- " ; // 可以自己增加可输入值
var i;
var c;
if (String.charAt( 0 ) == ' - ' ) // 如果第一个字符是‘-’则返回false
return false ;
if (String.charAt(String.length - 1 ) == ' - ' ) // 如果最后一个字符是‘-’,则返回false
return false ;
for (i = 0 ; i < String.length; i ++ ) {
c
= String.charAt(i);
if (Letters.indexOf(c) < 0 ) // 如果有Letters中不包含的字符则返回false
return false ;
}
return true ;
}
function CheckForm() {
if ( ! isNumber(document.getElementById( " Text1 " ).value)) {
alert(
" 您的电话号码不合法! " );
document.getElementById(
" Text1 " ).focus();
return false ;
}
return true ;
}

</ script >
< input id = " Text1 " type = " text " /></ br >
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

6,判断是否全是数字

View Code
< script language = " javascript " >


function checkNum(str) {

if (str.match( / \D / )) {
alert(
" 含有不是数字的值 " );
return false ;
}
alert(
" 全是数字 " );
return true ;
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

checkNum(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

7,判断字符和汉字

View Code
< script language = " javascript " >


function checkNum(str) {

if ( / [ ^ \x00 - \xff] / g.test(str)) {
alert(
" 含有汉字 " );
}
else alert( " 全是字符 " );
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

checkNum(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />
===========================================
或者
< script language = " javascript " >


function checkNum(str) {

if (escape(str).indexOf( " %u " ) != - 1 ) {
alert(
" 含有汉字 " );
}
else alert( " 全是字符 " );
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

checkNum(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

 8,判断是否是Email地址

View Code
// 函数名:chkemail
// 功能介绍:检查是否为Email Address
// 参数说明:要检查的字符串
// 返回值:0:不是 1:是
function chkemail(a) {
var i
= a.length;
var temp
= a.indexOf( ' @ ' );
var tempd
= a.indexOf( ' . ' );

if (temp > 1 ) {

if ((i - temp) > 3 ) {
if ((i - tempd) > 0 ) {
alert(
" 是Email地址 " );
return true ;
}

}
}
alert(
" Email地址有误 " );
return false ;
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

chkemail(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

9,数字格式校验

View Code
< script language = " javascript " >


// 函数名:fucCheckNUM
// 功能介绍:检查是否为数字
// 参数说明:要检查的数字
// 返回值:1为是数字,0为不是数字
function fucCheckNUM(NUM) {
var i, j, strTemp;
strTemp
= " 0123456789 " ;
if (NUM.length == 0 )
return 0
for (i = 0 ; i < NUM.length; i ++ ) {
j
= strTemp.indexOf(NUM.charAt(i));
if (j == - 1 ) {
// 说明有字符不是数字
alert( " 不是数字 " );
return false ;
}
}
// 说明是数字
alert( " 是数字 " );
return true ;
}

function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

fucCheckNUM(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

10,电话号码校验

View Code
< 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,判断是否是中文

View Code
< script language = " javascript " >


function ischinese(s) {
var ret
= true ;
for (var i = 0 ; i < s.length; i ++ ) {
if (s.charCodeAt(i) >= 10000 ) {
alert(
" 是中文 " );

}
else {
alert(
" 不是中文 " );
}
}

return false ;
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

ischinese(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

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

View Code
< script language = " javascript " >


function notchinese(str) {
var reg
= / [ ^ A - Za - z0 - 9_] / g
if (reg.test(str)) {
alert(
" 不含有数字字母下划线 " );
return false ;
}
else {
alert(
" 含有数字字母下划线 " );
return true ;
}
}
function CheckForm() {
var o
= document.getElementById( " Text1 " ).value;

notchinese(o);
}


</ script >
< input id = " Text1 " type = " text " />
< input id = " Button1 " type = " button " onclick = " CheckForm() " value = " button " />

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

View Code
< 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 " />

你可能感兴趣的:(JavaScript)