2013-08-29 38 views
1

我有一個asp:textbox從用戶處取得一個容器編號並驗證它,我如何確保輸入的字符串正好是11個字符,其中文本框輸入控件(字符數,字符類型)

  1. 前4個字符都是普通英語字符
  2. 的第四字符必須是下列{U或J或Z}
  3. 其餘(9個字符)中的一個是數字。

例> 「CRLU123456789」

+0

你可以使用的RegularExpressionValidator和可有人爲你煮了正則表達式吧。請參閱http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx – Yeronimo

+0

我想過,但問題是我不能寫它,我不好與RegularExpression – M009

回答

3

我會建議您使用RegularExpressionValidator。他們是非常容易的標記:

<asp:RegularExpressionValidator 
    ControlToValidate="NameOfTextBox" <!-- the input text box name --> 
    ValidationExpression="[a-zA-Z]{4}[UJZ]\d{9}" 
    ErrorMessage="Please enter a valid container number." 
    Display="Dynamic" <!-- this means it's display: none when no error --> 
    EnabledClientScript="true" <!-- this will perform JavaScript validation --> 
    runat="server" /> 

現在,隨着EnabledClientScript="true"它實際上將驗證用JavaScript控制,如果它的啓用,但因爲你仍然不能靠,你需要驗證服務器端太。所以,你要做的是在按鈕的點擊,要發生的驗證,第一個代碼塊應該是:

this.Validate(); // validates all Validators on the page 
if (!this.IsValid) { return; } 

,當你回來後會顯示錯誤消息。

1

使用RegularExpressionValidator控件與此ValidationExpression:[a-zA-Z]{4}[ujzUJZ][0-9]{9}

相關問題