javascript表单验证 net

二.<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
    
<script language =javascript >
    function check()
    
{
      
///注意javascript中正则表示式的写法:  /表示式/
      var rg= /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/;
      
     
if(document.getElementById("txtid").value=="000")
     
{
       
       alert(
"Can't Be 000");
       document.all(
"txtid").focus();
        
return false;
     }

 
      var str
=document.getElementById("txtmail").value;
      
if(!rg.test(str))
      
{
        alert(
"Email Addess error!!");
        document.all(
"txtmail").focus();
       
return false;
       }

     
else
     
{
       
return true;
     }

     
    
    }

    
</script>
</head>
<body>
    
<form id="form1" runat="server">
        
<asp:TextBox ID="txtmail" runat="server" Style="left: 270px; position: relative;
            top: 221px"></asp:TextBox>;
    <!--Notes: "OnClientClick" and "onclick"   The Use of The 'Return'-->
        
<asp:Button id="btuLogin" OnClientClick ="return check()" runat="server" Style="left: 211px; position: relative; top: 289px"
            Text
="Button" OnClick="btuLogin_Click"  />
        
<asp:TextBox ID="txtid"  runat="server" Style="left: 48px; position: relative;
            top: 159px"></asp:TextBox>&nbsp;
    </form>
</body>
</html>

二.

<script Language="JavaScript"> 
function FormCheck() 

if (trim(document.Form1.user.value) =="")
{
alert(
"请填写您的用户名!");
document.Form1.user.focus();
return false;
}

var filter=/^s*[A-Za-z0-9]{6,20}s*$/;
if (!filter.test(document.Form1.user.value)) 
alert(
"用户名填写不正确,请重新填写!可使用的字符为(A-Z a-z 0-9)长度不小于6个字符,不超过20个字符,注意不要使用空格。"); 
document.Form1.user.focus();
document.Form1.user.select();
return false
}
 
if (trim(document.Form1.pass.value) ==""
{
alert(
"请填写您的密码!");
document.Form1.pass.focus();
return false
}

var filter=/^s*[A-Za-z0-9]{6,20}s*$/;
if (!filter.test(document.Form1.pass.value)) 
alert(
"密码填写不正确,请重新填写!可使用的字符为(A-Z a-z 0-9 )长度不小于6个字符,不超过20个字符,注意不要使用空格。"); 
document.Form1.pass.focus();
document.Form1.pass.select();
return false
}
 
if(trim(document.Form1.confirmpassword.value) ==""){
alert(
"请输入您的确认密码!");
document.Form1.confirmpassword.focus();
return false;
}

if (document.Form1.pass.value!=document.Form1.confirmpassword.value ){
alert(
"两次填写的密码不一致,请重新填写!"); 
document.Form1.pass.focus();
document.Form1.pass.select();
return false
}
 
if (trim(document.Form1.question.value) ==""

alert(
"请选择密码提示问题!"); 
document.Form1.question.focus(); 
return (false); 
}
 
if (trim(document.Form1.answer.value) ==""

alert(
"请输入密码提示答案!"); 
document.Form1.answer.focus(); 
return (false); 
}
 
if (trim(document.Form1.name.value) ==""

alert(
"请输入您的真实姓名!"); 
document.Form1.name.focus(); 
return (false); 
}
 
var  check=true;
      
for(var i=0;i<document.Form1.ch.length;i++){
 
if (document.Form1.ch[i].checked) {
   check
=false;
  
break;
        }
 
       }

              
if (check){
          alert(
"请填写您的性别!");
          document.Form1.ch[
1].focus();
          
return (false);
       }
  
if (trim(document.Form1.email.value) =="")
{
alert(
"请输入您的电子邮件地址!");
document.Form1.email.focus();
document.Form1.email.select();
return false;
}

var str1 = document.Form1.email.value
if(str1.indexOf("@"== -1 || str1.indexOf("."== -1)
alert(
"E-mail格式不正确,请重新填写!"); 
document.Form1.email.focus();
document.Form1.email.select();
return false
}
 
if (trim(document.Form1.phonecountry.value) ==""

alert(
"请输入联系电话的国家区号!"); 
document.Form1.phonecountry.focus(); 
return (false); 
}
 
var filter=/^s*[0-9]{0,3}s*$/;
if (!filter.test(document.Form1.phonecountry.value)) 
alert(
"国家区号不正确,请重新填写!不超过3个数字,注意不要使用空格。"); 
document.Form1.phonecountry.focus();
document.Form1.phonecountry.select();
return false
}
 
if (trim(document.Form1.phonearea.value) ==""

alert(
"请输入联系电话的城市区号!"); 
document.Form1.phonearea.focus(); 
return (false); 
}
 
var filter=/^s*[0-9]{0,4}s*$/;
if (!filter.test(document.Form1.phonearea.value)) 
alert(
"城市区号不正确,请重新填写!不超过4个数字,注意不要使用空格。"); 
document.Form1.phonearea.focus();
document.Form1.phonearea.select();
return false
}
 
if (trim(document.Form1.phonenumber.value) ==""

alert(
"请输入您的联系电话号码!"); 
document.Form1.phonenumber.focus(); 
return (false); 
}

if (trim(document.Form1.country1.value) =="中国"

if (trim(document.Form1.province.value) =="")
{
alert(
"请选择省份!"); 
document.Form1.province.focus(); 
return (false); 
}

if (trim(document.Form1.city.value) =="")
{
alert(
"请选择地级城市!"); 
document.Form1.city.focus(); 
return (false); 
}

}
 
if (trim(document.Form1.address.value) ==""

alert(
"请输入您的联系地址!"); 
document.Form1.address.focus(); 
return (false); 
}
 
if (trim(document.Form1.qymc.value) ==""

alert(
"请输入贵公司的名称!"); 
document.Form1.qymc.focus(); 
return (false); 
}
 
if (trim(document.Form1.zw.value) ==""

alert(
"请输入您的职位!"); 
document.Form1.zw.focus(); 
return (false); 
}
 
if (trim(document.Form1.categoryid.value) =="")

alert(
"请选择主营行业!"); 
document.Form1.categoryid.focus(); 
return (false); 
}
 
if(trim(document.Form1.productionService.value) == ""
{
alert(
"主要相关产品名/服务名最少要填一个 并且按左至右的顺序填写");
document.Form1.productionService.focus();
return (false);
}

   
var productionService = Form1.productionService.value;
   
for(i=0;i<productionService.length;i++){productionService=productionService.replace(',','');}
    
if(Form1.productionService2.value){
        
for(i=0;i<Form1.productionService2.value.length;i++){Form1.productionService2.value=Form1.productionService2.value.replace(',','');}
        productionService
+=','+Form1.productionService2.value;
    }

    
if(Form1.productionService3.value){
        
for(i=0;i<Form1.productionService3.value.length;i++){Form1.productionService3.value=Form1.productionService3.value.replace(',','');}
        productionService
+=','+Form1.productionService3.value;
    }
    
    Form1.p_z_Z_productionService.value 
= productionService
 
document.Form1.submit()
}

//-->
//
-->
function trim(inputString) {
    
              
if (typeof inputString != "string"return inputString; }
              
var retValue = inputString;
              
var ch = retValue.substring(01);
              
while (ch == " "
           
//检查字符串开始部分的空格
                  retValue = retValue.substring(1, retValue.length);
                  ch 
= retValue.substring(01);
              }

              ch 
= retValue.substring(retValue.length-1, retValue.length);
              
while (ch == " "{
                 
//检查字符串结束部分的空格
                 retValue = retValue.substring(0, retValue.length-1);
                 ch 
= retValue.substring(retValue.length-1, retValue.length);
              }

              
while (retValue.indexOf("  "!= -1
          
//将文字中间多个相连的空格变为一个空格
                 retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
              }

              
return retValue;
           }
 
//--> 
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
教你怎么正确使用邮箱地址验证代码 下面是一些常用的正则表达式: 匹配中文字符的正则表达式: [\u4e00-\u9fa5] 评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff] 评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) 匹配空白行的正则表达式:\n\s*\r 评注:可以用来删除空白行 匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</\1>|<.*? /> 评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力 匹配首尾空白字符的正则表达式:^\s*|\s*$ 评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式 匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* 评注:表单验证时很实用 匹配网址URL的正则表达式:[a-zA-z]+://[^\s]* 评注:网上流传的版本功能很有限,上面这个基本可以满足需求 匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$ 评注:表单验证时很实用 匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7} 评注:匹配形式如 0511-4405222 或 021-87888822 匹配腾讯QQ号:[1-9][0-9]{4,} 评注:腾讯QQ号从10000开始 匹配中国邮政编码:[1-9]\d{5}(?!\d) 评注:中国邮政编码为6位数字 匹配身份证:\d{15}|\d{18} 评注:中国的身份证为15位或18位 匹配ip地址:\d+\.\d+\.\d+\.\d+ 评注:提取ip地址时有用 匹配特定数字: ^[1-9]\d*$ //匹配正整数 ^-[1-9]\d*$ //匹配负整数 ^-?[1-9]\d*$ //匹配整数 ^[1-9]\d*|0$ //匹配非负整数(正整数 + 0) ^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0) ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数 ^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数 ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数 ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0) ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0) 评注:处理大量数据时有用,具体应用时注意修正 匹配特定字符串: ^[A-Za-z]+$ //匹配由26个英文字母组成的字符串 ^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串 ^[a-z]+$ //匹配由26个英文字母的小写组成的字符串 ^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串 ^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值