前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="注册js验证.aspx.cs" Inherits="注册js验证" %>
<!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>
<style type="text/css">
.style1
{
width: 42%;
}
.style2
{
width: 197px;
}
#Button1
{
width: 78px;
}
#Ensure
{
width: 73px;
}
</style>
<script src="Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#txtname").blur(function () {
if ($(this).val().trim().length <= 6 || $(this).val().trim().length >= 12) { //trim() jQuery去掉字符串的首尾空格
alert("用户名应大于六位且小于12位");
}
})
$('#txtpassword').blur(function () {
if ($(this).val().trim().length == 0) {
alert("密码不能为空!");
}
})
$('#txtpasswordensure').blur(function () {
if ($(this).val().trim() != $('#txtpassword').val().trim()) {
alert("两次输入密码不一致!");
}
})
$('#txtshenfen').blur(function () {
var shenfen = $(this).val().trim();
if ((shenfen.length != 16 && shenfen.length != 18 || /^(\d+)?$/.test(shenfen) == false) && (/^(\d+)?$/.test(shenfen.substr(0, 15)) == false || shenfen.substring(15, 16) != 'X') && (/^(\d+)?$/.test(shenfen.substr(0, 17)) == false || shenfen.substring(17, 18) != 'X')) {
alert("请输入16位或18位有效身份证号!" + shenfen.length);
}
else {
alert('身份证填写成功!' + shenfen.length);
}
})
$('#txtemial').blur(function () {
//asdf@asdf.asdf
var mailvalue = $(this).val().trim();
var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
if (mailvalue == null || myreg.test(mailvalue)==false) {
alert('邮箱输入失败!');
}
else {
alert('邮箱输入成功!');
}
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td class="style2">
用户名:</td>
<td>
<input id="txtname" type="text" /></td>
</tr>
<tr>
<td class="style2">
密码:</td>
<td>
<input id="txtpassword" type="text" /></td>
</tr>
<tr>
<td class="style2">
确认密码:</td>
<td>
<input id="txtpasswordensure" type="text" /></td>
</tr>
<tr>
<td class="style2">
邮箱:</td>
<td>
<input id="txtemial" type="text" /></td>
</tr>
<tr>
<td class="style2">
身份证号:</td>
<td>
<input id="txtshenfen" type="text" /></td>
</tr>
<tr>
<td class="style2">
上传头像:</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Height="21px" οnclick="btnUpload_Click" Text="确认"
Width="85px" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class 注册js验证 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
//验证文件大小
if (this.FileUpload1.PostedFile.ContentLength/1024>7999)
{
this.Label1.Text = "文件长度过大,请重新上传!";
return;
}
if (this.FileUpload1.PostedFile.ContentType.Substring(0,5)=="image")
{
//获取文件的实际名称
string filereal = FileUpload1.FileName;
//生成文件名称
string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
//生成随机数
Random ran = new Random();
int num = ran.Next(0,10000);
filename += num;
//获取文件扩展名
string filenameextion = FileUpload1.FileName;
filenameextion = filenameextion.Substring(filenameextion.LastIndexOf("."));
filename += filenameextion;
//上传文件
FileUpload1.SaveAs(Server.MapPath(@"uploads\")+filename);
this.Label1.Text = "上传成功!";
}
else
{
this.Label1.Text = "上传的文件类型不符合规定,请重新上传";
}
}
}
js实现验证注册
最新推荐文章于 2022-07-19 20:57:49 发布