支援多驗證TextBox控制項

2022-07-29 13:30:16 字數 3738 閱讀 5232

using system;

using system.web.ui;

using system.web.ui.webcontrols;

using system.componentmodel;

using system.drawing;

using system.text;

using system.text.regularexpressions;

namespace rungoo.webctrllib

#endregion

/// attribute defaultproperty指定元件的預設屬性,toolboxdata指定當從ide工具中的工具箱中拖動自定義控制項時為它生成的預設標記

[defaultproperty("allowempty"), toolboxdata("<:webtextbox runat=server>")]

//類mycontrol派生自webcontrol

public class webtextbox : system.web.ui.webcontrols.textbox

set

}[bindable(true)]

[category("自定義資訊區")]

[browsable(true)]

[description("驗證資料型別,預設為不驗證")]

[defaultvalue("intpostive")]

public datatype validtype

set

}[bindable(true)]

[browsable(true)]

[category("自定義資訊區")]

[description("自定義驗證錯誤資訊")]

[defaultvalue("")]

public string validerror

set

}[bindable(true)]

[browsable(true)]

[category("自定義資訊區")]

[description("自定義用於驗證的正規表示式,validtype 為 custom 時有效")]

[defaultvalue("")]

public string validexpressioncustom

set

}[bindable(true)]

[browsable(true)]

[category("自定義資訊區")]

[description("錯誤資訊提示的css類名")]

[defaultvalue("")]

public string csserror

set

}#endregion

#region 建構函式

public webtextbox()

#endregion

#region ensurechildcontrols

protected override void ensurechildcontrols()

#endregion

/// /// 根據設定的驗證資料型別返回不同的正規表示式樣

///

///

#region getregex

private string getvalidregex()

\d*)";

break;

case datatype.intzeropostive:

error = "*必須為不小於0的整數";

regex = @"(\d+)";

break;

case datatype.float:

error = "*必須為數字";

regex = @"(-)?(\d+)(((\.)(\d)+))?";

break;

case datatype.floatpostive:

error = "*必須為大於0的數字";

regex = @"(\d+)(((\.)(\d)+))?";

break;

case datatype.floatzeropostive:

error = "*必須為不小於0的數字";

regex = @"(\d+)(((\.)(\d)+))?";

break;

case datatype.url:

error = "*url格式錯誤";

error = "*email格式錯誤";

regex = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";

break;

// case datatype.chinesechars :

// error = "*包含中文字元";

// regex = @"[^\x00-\xff]";

// break;

case datatype.englishchars:

error = "*只能輸入英文本元";

regex = @"[a-za-z]*";

break;

case datatype.engnum:

error = "*只能輸入英文本元和數字";

regex = @"[a-za-z0-9]*";

break;

case datatype.engnumunerline:

error = "*只能輸入英文本元、數字和下劃線";

regex = @"[a-za-z0-9_]*";

break;

case datatype.phonenumber:

error = "***號碼格式錯誤";

regex = @"(86)?(-)?(0\d)?(-)?(\d)(-)?(\d)?";

break;

case datatype.mobilenumber:

error = "*手機號碼格式錯誤";

regex = @"(0)?(13)\d";

break;

case datatype.postalcode:

error = "*郵編格式錯誤";

regex = @"\d";

break;

case datatype.custom:

error = "*格式錯誤";

regex = this.validexpressioncustom;

break;

default:

break;

}if (this.validerror.trim() != "")

error = this.validerror;

return regex;

}#endregion

#region 將此控制項呈現給指定的輸出引數

/// /// 將此控制項呈現給指定的輸出引數。

///

/// 要寫出到的 html 編寫器

protected override void render(htmltextwriter output)

if (this.validtype != datatype.never && this.validtype != datatype.string)

}#endregion

}}

TextBox控制項

1 獲取當前行第乙個索引值 int index textbox1.getfirstcharindexofcurrentline 2 獲取總行數 int line textbox1.lines.length 3 獲取指定字元位置檢索行號 int line textbox1.getlinefromcha...

TextBox控制項

第乙個不能在後台操作 文字框的多種形式 l 單行文字框 textmode singleline l 多行文字框 textmode mulitline l 密碼文字框 textmode password 文字框需要掌握的屬性 text 設定或獲取文字內容 textmode singleline,mul...

TextBox 控制項

textbox控制項上有乙個箭頭,multiline 屬性,是多行顯示 textbox控制項有 system.windows.textbox 類提供,提供了基本的文字輸入和編輯功能 屬性 acceptsretun控制按下回車鍵時顯示是換行還是啟用按鈕 false換行false是啟用 characte...