常用js函数

/*********************************************************************************
* javascript正则表达式检验
**********************************************************************************/


//校验是否全由数字组成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}

//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}

//校验用户姓名:只能输入1-30个以字母开头的字串
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}

//校验密码:只能输入6-20个字母、数字、下划线
function isPasswd(s)
{
var patrn=/^(/w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(s)
{
//var patrn=/^[+]{0,1}(/d){1,3}[ ]?([-]?(/d){1,12})+$/;
var patrn=/^[+]{0,1}(/d){1,3}[ ]?([-]?((/d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验手机号码:必须以数字开头,除数字外,可含有“-”
function isMobil(s)
{
var patrn=/^[+]{0,1}(/d){1,3}[ ]?([-]?((/d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}

//校验邮政编码
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}

//校验搜索关键字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|///][/]/{/}:;/'/,.<>/?]{1}[^`~!@$%^&()+=|///][/]/{/}:;/'/,.<>?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}

function isIP(s) //by zergling
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
 
 
/*********************************************************************************
* FUNCTION: isBetween
* PARAMETERS: val AS any value
* lo AS Lower limit to check
* hi AS Higher limit to check
* CALLS: NOTHING
* RETURNS: TRUE if val is between lo and hi both inclusive, otherwise false.
**********************************************************************************/
function isBetween (val, lo, hi) {
if ((val < lo) || (val > hi)) { return(false); }
else { return(true); }
}

/*********************************************************************************
* FUNCTION: isDate checks a valid date
* PARAMETERS: theStr AS String
* CALLS: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isDate (theStr) {
var the1st = theStr.indexOf('-');
var the2nd = theStr.lastIndexOf('-');

if (the1st == the2nd) { return(false); }
else {
var y = theStr.substring(0,the1st);
var m = theStr.substring(the1st+1,the2nd);
var d = theStr.substring(the2nd+1,theStr.length);
var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (y.length < 4) { return(false); }
else if (!isBetween (m, 1, 12)) { return(false); }
else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
else if (m==2) {
if (y % 4 > 0) maxDays = 28;
else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
else maxDays = 29;
}
if (isBetween(d, 1, maxDays) == false) { return(false); }
else { return(true); }
}
}
/*********************************************************************************
* FUNCTION: isEuDate checks a valid date in British format
* PARAMETERS: theStr AS String
* CALLS: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isEuDate (theStr) {
if (isBetween(theStr.length, 8, 10) == false) { return(false); }
else {
var the1st = theStr.indexOf('/');
var the2nd = theStr.lastIndexOf('/');

if (the1st == the2nd) { return(false); }
else {
var m = theStr.substring(the1st+1,the2nd);
var d = theStr.substring(0,the1st);
var y = theStr.substring(the2nd+1,theStr.length);
var maxDays = 31;

if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
return(false); }
else if (y.length < 4) { return(false); }
else if (isBetween (m, 1, 12) == false) { return(false); }
else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
else if (m==2) {
if (y % 4 > 0) maxDays = 28;
else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
else maxDays = 29;
}

if (isBetween(d, 1, maxDays) == false) { return(false); }
else { return(true); }
}
}

}
/********************************************************************************
* FUNCTION: Compare Date! Which is the latest!
* PARAMETERS: lessDate,moreDate AS String
* CALLS: isDate,isBetween
* RETURNS: TRUE if lessDate<moreDate
*********************************************************************************/
function isComdate (lessDate , moreDate)
{
if (!isDate(lessDate)) { return(false);}
if (!isDate(moreDate)) { return(false);}
var less1st = lessDate.indexOf('-');
var less2nd = lessDate.lastIndexOf('-');
var more1st = moreDate.indexOf('-');
var more2nd = moreDate.lastIndexOf('-');
var lessy = lessDate.substring(0,less1st);
var lessm = lessDate.substring(less1st+1,less2nd);
var lessd = lessDate.substring(less2nd+1,lessDate.length);
var morey = moreDate.substring(0,more1st);
var morem = moreDate.substring(more1st+1,more2nd);
var mored = moreDate.substring(more2nd+1,moreDate.length);
var Date1 = new Date(lessy,lessm,lessd);
var Date2 = new Date(morey,morem,mored);
if (Date1>Date2) { return(false);}
return(true);

}

/*********************************************************************************
* FUNCTION isEmpty checks if the parameter is empty or null
* PARAMETER str AS String
**********************************************************************************/
function isEmpty (str) {
if ((str==null)||(str.length==0)) return true;
else return(false);
}

/*********************************************************************************
* FUNCTION: isInt
* PARAMETER: theStr AS String
* RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE
* CALLS: isDigit
**********************************************************************************/
function isInt (theStr) {
var flag = true;

if (isEmpty(theStr)) { flag=false; }
else
{ for (var i=0; i<theStr.length; i++) {
if (isDigit(theStr.substring(i,i+1)) == false) {
flag = false; break;
}
}
}
return(flag);
}

/*********************************************************************************
* FUNCTION: isReal
* PARAMETER: heStr AS String
decLen AS Integer (how many digits after period)
* RETURNS: TRUE if theStr is a float, otherwise FALSE
* CALLS: isInt
**********************************************************************************/
function isReal (theStr, decLen) {
var dot1st = theStr.indexOf('.');
var dot2nd = theStr.lastIndexOf('.');
var OK = true;

if (isEmpty(theStr)) return false;

if (dot1st == -1) {
if (!isInt(theStr)) return(false);
else return(true);
}

else if (dot1st != dot2nd) return (false);
else if (dot1st==0) return (false);
else {
var intPart = theStr.substring(0, dot1st);
var decPart = theStr.substring(dot2nd+1);

if (decPart.length > decLen) return(false);
else if (!isInt(intPart) || !isInt(decPart)) return (false);
else if (isEmpty(decPart)) return (false);
else return(true);
}
}

/*********************************************************************************
* FUNCTION: isEmail
* PARAMETER: String (Email Address)
* RETURNS: TRUE if the String is a valid Email address
* FALSE if the passed string is not a valid Email Address
* EMAIL FORMAT: AnyName@EmailServer e.g; webmaster@hotmail.com
* @ sign can appear only once in the email address.
*********************************************************************************/
function isEmail (theStr) {
var atIndex = theStr.indexOf('@');
var dotIndex = theStr.indexOf('.', atIndex);
var flag = true;
theSub = theStr.substring(0, dotIndex+1)

if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length))
{ return(false); }
else { return(true); }
}
/*********************************************************************************
* FUNCTION: newWindow
* PARAMETERS: doc -> Document to open in the new window
hite -> Height of the new window
wide -> Width of the new window
bars -> 1-Scroll bars = YES 0-Scroll Bars = NO
resize -> 1-Resizable = YES 0-Resizable = NO
* CALLS: NONE
* RETURNS: New window instance
**********************************************************************************/
function newWindow (doc, hite, wide, bars, resize) {
var winNew="_blank";
var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,";
opt+=("scrollbars="+bars+",");
opt+=("resizable="+resize+",");
opt+=("width="+wide+",");
opt+=("height="+hite);
winHandle=window.open(doc,winNew,opt);
return;
}
/*********************************************************************************
* FUNCTION: DecimalFormat
* PARAMETERS: paramValue -> Field value
* CALLS: NONE
* RETURNS: Formated string
**********************************************************************************/
function DecimalFormat (paramValue) {
var intPart = parseInt(paramValue);
var decPart =parseFloat(paramValue) - intPart;

str = "";
if ((decPart == 0) || (decPart == null)) str += (intPart + ".00");
else str += (intPart + decPart);

return (str);
}

 

"^//d+$"  //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$"  //正整数
"^((-//d+)|(0+))$"  //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$"  //负整数
"^-?//d+$"    //整数
"^//d+(//.//d+)?$"  //非负浮点数(正浮点数 + 0)
"^(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数
"^((-//d+(//.//d+)?)|(0+(//.0+)?))$"  //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+//.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*//.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数
"^(-?//d+)(//.//d+)?$"  //浮点数
"^[A-Za-z]+$"  //由26个英文字母组成的字符串
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串
"^[a-z]+$"  //由26个英文字母的小写组成的字符串
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串
"^//w+$"  //由数字、26个英文字母或者下划线组成的字符串
"^[//w-]+(//.[//w-]+)*@[//w-]+(//.[//w-]+)+$"    //email地址
"^[a-zA-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$"  //url 

asp.net常用的javascript经典例子
Dropdownlist无刷新的例子。xml.
例如文本验证!
下面是一个单选按纽前台不刷新的例子.有好的就往上贴
<script language="javascript">
   function SetButton()  
   {
    if(document.all.rdoByHuman.checked==true)
    {
     document.all.cboHrPut.disabled = "";
     document.all.cboAnswerHr.disabled = "";
    
         
     document.all.cboGroup.disabled = true;
    
    }
    if(document.all.rdoByGroup.checked==true)
    {
     document.all.cboHrPut.disabled = true;
     document.all.cboAnswerHr.disabled = true;
         
     document.all.cboGroup.disabled = "";
    
    
   
    }
   }
 </script>
 
 
<asp:RadioButton id="rdoByHuman" οnclick="SetButton();" runat="server" GroupName="TotalGroup" Checked="True" Text="按人员"></asp:RadioButton>
e.Item.Attributes.Add("onmouseOver","this.style.backgroundColor='#dee3e7'");//鼠标移上去的颜色
   e.Item.Attributes.Add("onmouseOut","this.style.backgroundColor='white'");
   e.Item.Cells[2].Attributes.Add("onmouseOver","this.style.backgroundColor='red'");
   e.Item.Cells[2].Attributes.Add("onmouseOut","this.style.backgroundColor='white'");//指定某列的颜色
   e.Item.Cells[3].Style["cursor"]="hand";
   e.Item.Cells[4].Attributes.Add("onclick","alert('你惦记的ID 是:"+e.Item.Cells[4].Text+"');");//指定显示字段
  e.Item.Cells[1].Attributes.Add("title","'红孩是未解决的,蓝精灵是已解决的!'"+e.Item.Cells[0].Text.ToString());//显示title
   e.Item.Cells[0].Attributes.Add("onclick","window.open('xinxiForm.aspx?idmain="+e.Item.Cells[0].Text+"','','ToolBar=no,width=260,height=200');");
 
 
屏闭一些键:
 
function KeyDown(){    //屏蔽鼠标右键、Ctrl+n、shift+F10、F5刷新、退格键
   //alert("ASCII代码是:"+event.keyCode);
 if ((window.event.altKey)&&
      ((window.event.keyCode==37)||   //屏蔽 Alt+ 方向键 ←
       (window.event.keyCode==39))){ //屏蔽 Alt+ 方向键 →
     alert("不准你使用ALT+方向键前进或后退网页!");
     event.returnValue=false;
     }
 if ((event.keyCode==8) ||                  //屏蔽退格删除键
      (event.keyCode==116)||                 //屏蔽 F5 刷新键
      (event.keyCode==112)||                 //屏蔽 F1 刷新键
      (event.ctrlKey && event.keyCode==82)){ //Ctrl + R
     event.keyCode=0;
     event.returnValue=false;
     }
 if ((event.ctrlKey)&&(event.keyCode==78))    //屏蔽 Ctrl+n
     event.returnValue=false;
 if ((event.shiftKey)&&(event.keyCode==121)) //屏蔽 shift+F10
      event.returnValue=false;
 if (window.event.srcElement.tagName == "A" && window.event.shiftKey) 
      window.event.returnValue = false; //屏蔽 shift 加鼠标左键新开一网页
 if ((window.event.altKey)&&(window.event.keyCode==115)){ //屏蔽Alt+F4
      window.showModelessDialog("about:blank","","dialogWidth:1px;dialogheight:1px");
      return false;}
}
1. οncοntextmenu="window.event.returnvalue=false"   将彻底屏蔽鼠标右键 
2. <body onselectstart="return false">         取消选取、防止复制 
3. οnpaste="return false"               不准粘贴 
4. οncοpy="return false;" oncut="return false;"    防止复制;防止剪切
5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标 
6. <link rel="Bookmark" href="favicon.ico">      可以在收藏夹中显示出你的图标 
7. <input style="ime-mode:disabled">             关闭输入法
 
8. 永远都会带着框架 
<script language="javascript"><!-- 
 if (window==top)
 top.location.href="frames.htm"; file://frames.htm为框架网页 
// --></script>
 
9. 防止被人frame 
<SCRIPT LANGUAGE=javascript><!--   
 if (top.location!=self.location)
 top.location=self.location; 
// --></SCRIPT> 
 
10. <noscript><iframe src=*.html></iframe></noscript>  网页将不能被另存为
 
 
使用鼠标拖动的层
<BODY BGCOLOR="#FFFFFF">
<div οnmοusedοwn="style.cursor='move';startMove(this)" οnmοuseup="style.cursor='auto'" style="border:1px solid #AAAAAA;background-color:#EEEEEE;width:300;height:200;position;text-align:center;">可以使用鼠标拖动</div>
<script language=javascript>
function startMove(objDiv)
{
 document.attachEvent("onmousemove",moveDiv);
 document.attachEvent("onmouseup",endMove);
 document.attachEvent("onselectstart",selectNo);
 document["moveDiv"] = objDiv;
 document["startX"] = event.x;
 document["startY"] = event.y;
 document["oldX"] = objDiv.getBoundingClientRect().left;
 document["oldY"] = objDiv.getBoundingClientRect().top;
}
function moveDiv()
{
 //try{
 var obj = document["moveDiv"];
 if(obj)
 {
 var l = document["oldX"];//obj.getBoundingClientRect().left;//obj.style.left// = 100//(event.x-document["startX"]);
 var t = document["oldY"];//obj.getBoundingClientRect().top;//obj.style.top// = 100//(event.y-document["startY"]);
 obj.style.position = "absolute";
 obj.style.left = l + (event.x-document["startX"]);
 obj.style.top = t + (event.y-document["startY"]);
 }
 //}catch(e){endMove();}
}
function endMove()
{
 document.detachEvent("onmousemove",moveDiv);
 document.detachEvent("onmouseup",endMove);
 document.detachEvent("onselectstart",selectNo);
 document["moveDiv"] = null;
 document["startX"] = null;
 document["startY"] = null;
}
function selectNo()
{ return false; }
</script>
</BODY>
 
 
/* 异步,动态的加载网页xml数据 */
//实际运用有更改
function GetXml(objContainer,id){
 var XmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
 objContainer.innerHTML=StateXML(Config.loading)
 objContainer.send="true"
 XmlHttp.onreadystatechange=function(){
 if(XmlHttp.readyState==4){
   if(XmlHttp.status==200){
   var Xmldoc=XmlHttp.responseXML
    if(Xmldoc.documentElement.hasChildNodes())
    objContainer.innerHTML=Xmldoc.transformNode(xsldoc)
    else
    objContainer.innerHTML=StateXML(Config.loading)
   }
   else
   objContainer.innerHTML=StateXML(Config.unavaible)
 }
 }
 XmlHttp.open("get",Config.Service+'?id='+id+'&tem'+Math.random(),true)
 XmlHttp.send()
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值