您的位置:首页 > Web前端 > JavaScript

表单验证javascript函数总结

2006-09-15 21:13 363 查看
开发网站用得最多的代码就是表单验证代码,为方便大家的学习和工作,特将一些常用的验证代码函数贴出来,供大家参考:(本文将不断更新)





Array.prototype.max = function()
{
var i, max = this[0];

for( i = 1; i < this.length; i++ )
{
if( max < this[i] )
max = this[i];
}

return max;
}

String.prototype.trim = function()
{

return this.replace( /(^/s*)|(/s*$)/g, "" );
}

function checkExp( re, s )
{
return re.test( s );
}

function isAlphaNumeric( strValue )
{

return checkExp( /^/w*$/gi, strValue );
}

function isDate( strValue )
{

if( isEmpty( strValue ) ) return true;

if( !checkExp( /^/d{4}-[01]?/d-[0-3]?/d$/g, strValue ) ) return false;


var arr = strValue.split( "-" );
var year = arr[0];
var month = arr[1];
var day = arr[2];


if( !( ( 1<= month ) && ( 12 >= month ) && ( 31 >= day ) && ( 1 <= day ) ) )
return false;


if( !( ( year % 4 ) == 0 ) && ( month == 2) && ( day == 29 ) )
return false;


if( ( month <= 7 ) && ( ( month % 2 ) == 0 ) && ( day >= 31 ) )
return false;


if( ( month >= 8) && ( ( month % 2 ) == 1) && ( day >= 31 ) )
return false;


if( ( month == 2) && ( day >=30 ) )
return false;

return true;
}

function isEmail( strValue )
{

if( isEmpty( strValue ) ) return true;


var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(/.[a-zA-Z0-9_-])+/;
return checkExp( pattern, strValue );

}

function isEmpty( strValue )
{
if( strValue == "" )
return true;
else
return false;
}

function isNumeric( strValue )
{


return checkExp( /^/d*$/g, strValue );
}

function ZisNumeric( strValue )
{

return !//D+/.test(strValue)
//return checkExp( /^/d*$/g, strValue );
}

function isMoney( strValue )
{

if( isEmpty( strValue ) ) return true;

return checkExp( /^[+-]?/d+(,/d{3})*(/./d+)?$/g, strValue );
}

function isPhone( strValue )
{

if( isEmpty( strValue ) ) return true;

return checkExp( /(^/(/d{3,5}/)/d{6,8}(-/d{2,8})?$)|(^/d+-/d+$)|(^(130|131|135|136|137|138|139)/d{8}$)/g, strValue );
}

function isMobile( strValue )
{

if( isEmpty( strValue ) ) return true;
return checkExp(/^13/d{9}$/, strValue );

}
function isPostalCode( strValue )
{

return checkExp( /(^$)|(^/d{6}$)/gi, strValue )
}

function isURL( strValue )
{

if( isEmpty( strValue ) ) return true;

var pattern = /^(http|https|ftp):////(/w+/.)+[a-z]{2,3}(///w+)*(///w+/./w+)*(/?/w+=/w*(&/w+=/w*)*)*/gi;


return checkExp( pattern, strValue );

}

function checkLength( strValue, strParam )
{
if( isEmpty( strValue ) ) return true;


if( strParam.charAt( 0 ) != 'L' ) return false;

var l = strValue.length;
var ml = parseInt( strParam.substr( 2 ) );

switch( strParam.charAt( 1 ) )
{
case '<' :
if( l >= ml )
return false;
break;

case '=' :
if( l != ml )
return false;
break;

case '>' :
if( l <= ml )
return false;
break;

default :
return false
}

return true;
}

function ValidateMaxLength( strName, strDescription, strLength) {
var strMsg = "";
var strValue = document.all( strName ).value.trim();
var strMaxLength = "L<" + strLength;
if( !checkLength( strValue, strMaxLength ))
strMsg = '"' + strDescription + '" 必须小于'+ strLength + '个字符/n';
return strMsg;
}

function ValidateMinLength( strName, strDescription, strLength) {
var strMsg = "";
var strValue = document.all( strName ).value.trim();
var strMaxLength = "L>" + strLength;
if( !checkLength( strValue, strMaxLength ))
strMsg = '"' + strDescription + '" 必须大于'+ strLength + '个字符/n';
return strMsg;
}

function ValidateEquLength( strName, strDescription, strLength) {
var strMsg = "";
var strValue = document.all( strName ).value.trim();
var strMaxLength = "L=" + strLength;
if( !checkLength( strValue, strMaxLength ))
strMsg = '"' + strDescription + '" 必须等于'+ strLength + '个字符/n';
return strMsg;
}





function Validate( strName, strDescription, strType)
{
var strMsg = "";
var strValue = document.all( strName ).value.trim();
var arrType = strType.split( " " );

for( var i = 0; i < arrType.length; i++ )
switch( arrType[i] )
{
case "AlphaNumeric" :
if( !isAlphaNumeric( strValue ) )
strMsg = '"' + strDescription + '" 必须是字母或数字!/n';
break;

case "Date" :
if( !isDate( strValue ) )
strMsg = '"' + strDescription + '" 必须具有正确的日期格式,如 2001-10-1/n';
break;

case "Email" :
if( !isEmail( strValue ) )
strMsg = '"' + strDescription + '" 必须具有正确的邮件格式!/n';
break;

case "NotEmpty" :
if( isEmpty( strValue ) )
strMsg = '"' + strDescription + '" 不能为空!/n';
break;

case "Numeric" :
if( !isNumeric( Math.abs(strValue) ) )
strMsg = '"' + strDescription + '" 必须是数字!/n';
break;
case "INumeric" :
if( !isNumeric( strValue ) )
strMsg = '"' + strDescription + '" 必须是大于或等于零数字!/n';
break;
case "Money" :
if( !isMoney( strValue ) )
strMsg = '"' + strDescription + '" 必须具有正确的货币格式,如 -123,456.789/n';
break;
case "ZisNumeric" :
if( !ZisNumeric( strValue ) )
strMsg = '"' + strDescription + '" 必须是数字!/n';
break;
case "Phone" :
if( !isPhone( strValue ) )
strMsg = '"' + strDescription + '" 必须具有正确的电话格式,如 (0755)1234567-999/n';
break;
case "isMobile" :
if( !isMobile( strValue ) )
strMsg = '"' + strDescription + '" 必须输入正确的手机号码/n';
break;
case "PostalCode" :
if( !isPostalCode( strValue ) )
strMsg = '"' + strDescription + '" 必须是6位数字!/n';
break;

case "URL" :
if( !isURL( strValue ) )
strMsg = '"' + strDescription + '" 必须是正确的URL格式!/n';
break;

default :
if( arrType[i].charAt( 0 ) == 'L' )
{
if( !checkLength( strValue, arrType[i] ) )
strMsg = '"' + strDescription + '" 的长度必须 ' + arrType[i].substr(1) + '/n';
}
else
strMsg = '错误:"' + strDescription + '" 的类型 "' + strType + '" 不能识别!/n';
}

return strMsg;
}



function confirm_delete( url )
{
if( confirm( "您确实要删除吗?" ) )
{
window.location = ( url )
}
}



function openNewWin( url, width, height,top,left)
{
var newwin = window.open( url, "NewWin", "scrollbars=yes,width=" + width + ",height=" + height +"" );
newwin.moveTo(top,left);
newwin.focus();
return false;
}

function opwin(url,name, width, height,top,left) { //v2.0
var newwinop = window.open(url,name,''+'width='+width+',height='+height+',scrollbars=yes'+'');
newwinop.moveTo(top,left);
return false;
}
function opwin2(url,name, width, height,top,left) { //v2.0
var newwinop = window.open(url,name,''+'width='+width+',height='+height+',scrollbars=no'+'');
newwinop.moveTo(top,left);
return false;
}

function openNewWin2( url, width, height,top,left)
{
var newwin2 = window.open( url, "NewWin2", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height +"" );
newwin2.moveTo(top,left);
newwin2.focus();
return false;
}
function openNewWin3( url, width, height,top,left)
{
var newwin3 = window.open( url, "newwin3", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,width=" + width + ",height=" + height +"" );
newwin3.moveTo(top,left);
newwin3.focus();
return false;
}
function openDialog( url, width, height)
{
showModalDialog( url, "NewWin","dialogWidth:"+ width +";dialogHeight:"+ height +";dialogTop:100;dialogLeft:200;status:no;");
}

var checkboxflag = "false";
function check(field)
{
if (checkboxflag == "false")
{
if(field.length == null)
{
if (field.disabled != true)
{
field.checked = true;
}
}
else
{
for (i = 0; i < field.length; i++)
{
if (field[i].disabled != true)
{
field[i].checked = true;
}
}
}
checkboxflag = "true";
return "全不选";
}
else
{
if(field.length == null)
{
if (field.disabled != true)
{
field.checked = false;
}
}
else
{
for (i = 0; i < field.length; i++)
{
if (field[i].disabled != true)
{
field[i].checked = false;
}
}
}
checkboxflag = "false";
return "全选";
}
}

function chkinverse(field)
{
if(field.length == null)
{
if (field.disabled != true)
{
if(field.checked == true)
{
field.checked = false;
}
else
{
field.checked = true;
}
}
}
else
{

for(i = 0; i < field.length; i++)
{
if (field[i].disabled != true)
{
if(field[i].checked == true)
{
field[i].checked = false;
}
else
{
field[i].checked = true;
}
}
}
}
return "反选"
}

//
function ActionConfirm(form,msg,field)
{
var flag=0;
var truthBeTold;
for(i = 0; i < field.length; i++)
{
if (field[i].disabled != true)
{
if(field[i].checked == true)
{
flag=1;
}
}
}

if(field.length == null)
{
if(field.checked == true)
{
flag=1;
}
}

if (flag==0)
{alert("请选择记录!");}
else
{
truthBeTold =window.confirm("你确定要["+msg+"]吗?");
if (truthBeTold) {
form.DoType.value=msg;
form.submit();
}
}
}

var isNav, isIE
if (parseInt(navigator.appVersion) >= 4) {
if (navigator.appName == "Netscape") {
isNav = true
} else {
isIE = true
}
}
function uf_mouseon(obj)
{
if (isIE)
{
//obj.filters.alpha.opacity = 70
//obj.filters.Gray.enabled = true;

}
}
function uf_mouseoff(obj)
{
if (isIE)
{
//obj.filters.alpha.opacity = 100
//obj.filters.Gray.enabled = false;
}

}

function PopWin(url)
{
window.open(url,'WindowResult','Width=480,Height=265,Resizable=1,scrollbars=1');
}

function change_sendmode()
{
var username
username=vote.username.value;

url="incuser.asp?username=" + username;
window.open(url,"frmGetSendmode");
}

function formCheck() {

if ("" == document.searchnews.newskeyword.value) {
alert("请输入你要查询的关键字")
document.searchnews.newskeyword.focus()
return false;
}
}
function chkkey(str)
{
fibkey = new Array ("guest" ,"七彩虹", "colorful","世和","qicaihong","seethru","sego","qch");
i=fibkey.length;
for (ii=0;ii<i ;ii++ )
{ //alert(fibkey[ii]);
if(str.indexOf(fibkey[ii])>-1)
{return 0;}
}
return 1;
}

function chksafe(a)
{
fibdn = new Array ("'",",",";", "/","<",">",":","//","!"," ","$","%","^","&","*","(",")");
i=fibdn.length;
j=a.length;
for (ii=0;ii<i;ii++)
{ for (jj=0;jj<j;jj++)
{ temp1=a.charAt(jj);
temp2=fibdn[ii];
if (temp1==temp2)
{ return 0; }
}
}
return 1;
}


function checkemail(id)
{
var s=id.value
var report;
if(isempty(id))
{
var i = 1;
var len = s.length;
if (len > 60)
{
alert("Email地址的长度不能超过60!/n");
id.select();
return false;
}
pos1 = s.indexOf("@");
pos2 = s.indexOf(".");
pos3 = s.lastIndexOf("@");
pos4 = s.lastIndexOf(".");
if ((pos1 <= 0)||(pos1 == len)||(pos2 <= 0)||(pos2 == len))
{
alert("请输入正确的Email地址/n");
id.select();
return false;
}
else
{
if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
|| ( pos1 != pos3 ) //find two @
|| ( pos4 < pos3 ) ) //. should behind the '@'
{
alert("请输入正确的Email地址,只能包含一个@/n");
id.select();
return false;
}
}
if ( !onlyinclude( s, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@"))
{
alert("Email只能包含ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_@这些字符/n" );
id.select();
return false;
}
}
else
{
alert("请输入正确的Email地址/n");
id.select();
return false;
}
return true;
}

function getRadioSelectedIndex(radio)
{
var dd=-1;
for(i=0;i<radio.length;i++){
if(radio[i].checked)dd=i;
}
return dd;
}
function getcheckboxSelectedIndex(checkbox)
{
var dds=-1;
for(i=0;i<checkbox.length;i++){
if(checkbox[i].checked)dds=i;
}
return dds;
}
function CheckRadioSelectedbak(radio,sName)
{
if(getRadioSelectedIndex(radio)<0){
alert("请选择"+sName+"!");
radio[0].focus();
return false;
}
return true;
}
function CheckRadioSelectedbak(radio,sName)
{
if(getRadioSelectedIndex(radio)<0){

radio[0].focus();
return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: