c#后台输出javascript语句示例程序
using System;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
namespace MyOraComm
{
/// <summary>
/// FuncTion 的摘要说明。
/// </summary>
public class Function
{
public Function()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
// ====== ==============================================================================
//=========================前台输出语句方法================================
//=======================================================================================
#region 弹出javascript对话框,是否返回或结束。
public void WriteMessage(string strMsg,bool Back,bool End)
{
HttpContext Context=HttpContext.Current;
strMsg=strMsg.Replace("'","");
strMsg=strMsg.Replace("/r/n","");
if(strMsg!=""&&strMsg!=null)
Context.Response.Write("<script language=javascript>alert('"+strMsg+"');</script>");
if(Back)
Context.Response.Write("<script language=javascript>history.back();</script>");
if(End)
Context.Response.End();
}
#endregion
#region 写javascript语句内容,<script language=javascript></script>已经写好。
public void WriteJavaScript(string strJavaScript)
{
HttpContext Context=HttpContext.Current;
Context.Response.Write("<script language=javascript>"+strJavaScript+"</script>");
}
#endregion
#region 关闭当前页面
public void CloseWindow()
{
HttpContext Context=HttpContext.Current;
Context.Response.Write("<script language=javascript>window.close();</script>");
Context.Response.End();
}
#endregion
//====================================================================================
//=========================使用正则表达式写的验证类方法================================
//=====================================================================================
#region 用正则表达式实现.验证输入是否是数字
public bool IsValidNumer(string str)
{
System.Text.RegularExpressions.Regex reg1
= new System.Text.RegularExpressions.Regex(@"^[-]?/d+[.]?/d*$");
return reg1.IsMatch(str);
}
#endregion
#region 验证是否为小数
public bool IsValidDecimal(string str)
{
return Regex.IsMatch(str,@"[0]./d{1,2}|[1]");
}
#endregion
#region 验证Email地址
public bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([/w-/.]+)@((/[[0-9]{1,3}/.[0-9]{1,3}/.[0-9]{1,3}/.)|(([/w-]+/.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(/]?)$");
}
#endregion
#region dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
public string MDYToDMY(String input)
{
return Regex.Replace(input,"//b(?//d{1,2})/(?//d{1,2})/(?//d{2,4})//b","${day}-${month}-${year}");
}
#endregion
#region 验证是否为电话号码
public bool IsValidTelNum(string strIn)
{
return Regex.IsMatch(strIn,@"(/d+-)?(/d{4}-?/d{7}|/d{3}-?/d{8}|^/d{7,8})(-/d+)?");
}
#endregion
#region 验证年月日
bool IsValidDate(string strIn)
{
return Regex.IsMatch(strIn,@"^2/d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]/d|3[0-1])(?:0?[1-9]|1/d|2[0-3])?:0?[1-9]|[1-5]/d)?:0?[1-9]|[1-5]/d)$");
}
#endregion
#region 验证后缀名
bool IsValidPostfix(string strIn)
{
return Regex.IsMatch(strIn,@"/.(?i:gif|jpg)$");
}
#endregion
#region 验证字符是否在4至12之间
bool IsValidByte(string strIn)
{
return Regex.IsMatch(strIn,@"^[a-z]{4,12}$");
}
#endregion
#region 验证IP
bool IsValidIp(string strIn)
{
return Regex.IsMatch(strIn,@"^(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$");
}
#endregion
}
}