2017-5-26 正则表达式,js实战(加减数量,全选)

Posted Zoe

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017-5-26 正则表达式,js实战(加减数量,全选)相关的知识,希望对你有一定的参考价值。

1、正则表达式:验证数据格式的准确性。

用正则表达式判断手机号格式是否正确:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:textbox ID="Textbox1" runat="server"></asp:textbox>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:button ID="Button1" runat="server" text="Button" />

    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    document.getElementById("Button1").onclick = function ()
    {
        document.getElementById("Label1").innerHTML = "";
        var s = document.getElementById("Textbox1").value;
        if (!/^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/.test(s));
        {
            document.getElementById("Label1").innerHTML = "格式错误!";
            return false;
        }
        return false;
    }

</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

2、用加减按钮增加减少数量

 

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn_jia" value="+" runat="server" Text="+" />
        <asp:TextBox ID="TextBox1" runat="server" style="width:30px;"></asp:TextBox>
        <asp:Button ID="btn_jian" value="-" runat="server" Text="-" />
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    document.getElementById("btn_jia").onclick = function ()
    {
        var s = document.getElementById("TextBox1");
        s.value = parseInt(s.value) + 1;
    }
    document.getElementById("btn_jian").onclick = function () {
        var s = document.getElementById("TextBox1");
        if (s.value <= 1) { return;}
        s.value = parseInt(s.value) - 1;
    }
</script>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = TextBox1.Text;
    }
}

3、用checkbox实现全选

 

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table style="width:100%;text-align:center;background-color:navy;">
            <tr style="color:white;">
                <td>
                    <input type="checkbox" id="ckall"/>
                    全选</td>
                <td>编号</td>
                 <td>姓名</td>
                 <td>密码</td>
                 <td>昵称</td>
                 <td>性别</td>
                <td>生日</td>
                <td>民族</td>
            </tr>
            
        
        <asp:Repeater ID="Repeater1" runat="server">
            <ItemTemplate>
                  <tr style="background-color:white;">
                      <td>
                          <input type="checkbox" name="ck" value="<%#Eval("ids") %>"/></td>
                <td><%#Eval("ids") %></td>
                 <td><%#Eval("username") %></td>
                 <td><%#Eval("password") %></td>
                 <td><%#Eval("nickname") %></td>
                 <td><%#Eval("sex") %></td>
                <td><%#Eval("birthday") %></td>
                <td><%#Eval("nation") %></td>
            </tr>


            </ItemTemplate>

        </asp:Repeater>
           
            </table>
         <asp:Button ID="Button1" runat="server" Text="Button" />
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    document.getElementById("ckall").onclick = function ()
    {
        var cks = document.getElementsByName("ck");
        for (var i = 0; i < cks.length; i++)
        {
            cks[i].checked=this.checked;
        }
        var ckss = document.getElementsByName("ck");
        for (var i = 0; i < ckss.length; i++)
        {
            ckss[i].onclick = function ()
            {   var count=0;
                for (var j = 0; j < ckss.length; j++)
                {
                    if (ckss[j].checked == false)
                    { count++;}
                }
                if (count == 0) {
                    document.getElementById("ckall").checked = true;
                }
                else
                {
                    document.getElementById("ckall").checked = false;
                }
            }
        }
    }

</script>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Repeater1.DataSource = new user1data().selectall();
            Repeater1.DataBind();
        }
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = Request["ck"];
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// user1data 的摘要说明
/// </summary>
public class user1data
{
    DataClassesDataContext con = new DataClassesDataContext();
    public user1data()
    {
    }
    public List<user1> selectall() 
    {
        return con.user1.ToList();
    }
}

 

以上是关于2017-5-26 正则表达式,js实战(加减数量,全选)的主要内容,如果未能解决你的问题,请参考以下文章

js 正则表达式:密码必须由6-12位数字加字母组成

JS --正则表达式验证实战之邮箱模式

Regex js 的正则

js 正则表达式 \\-是啥意思

js正则匹配总结

js关于正则表达式