2016-04-02 19 views
1

テキストボックスに文字列があるかどうかを確認したい文字列がない場合はエラーメッセージを表示するこのコードを試したが、テキストボックスに文字列を入力するとエラーメッセージ表示テキストボックスに文字列が含まれているかどうかをチェック

htmlコード:コードの後ろに

<label for="edit-submitted-name">First Name </label> 
 
<asp:TextBox ID="txtfirstname" runat="server" CssClass="form-text" size="60" maxlength="128"></asp:TextBox> 
 
    <asp:CompareValidator ID="cvfirstname" 
 
     runat="server" ErrorMessage="Must be letters" 
 
     ControlToValidate="txtfirstname" ForeColor="#db0d15" Type="String"></asp:CompareValidator>

protected void btnsave_Click(object sender, EventArgs e) 
     { 

      using (DataClasses1DataContext sdc = new DataClasses1DataContext()) { 


        Professor_Dim prof = sdc.Professor_Dims.SingleOrDefault(x => x.P_ID ==Convert.ToInt16(Server.UrlDecode(Request.QueryString["id"]))); 
       if (!string.IsNullOrEmpty(txtfirstname.Text)) 
        prof.P_Fname = txtfirstname.Text; 
       if (!string.IsNullOrEmpty(txtlastname.Text)) 
        prof.P_Lname = txtlastname.Text; 
       if (!string.IsNullOrEmpty(txtemail.Text)) 
        prof.P_Email = txtemail.Text; 
       if (!string.IsNullOrEmpty(txtaddress.Text)) 
        prof.P_Address = txtaddress.Text; 
       if (!string.IsNullOrEmpty(txtphone.Text)) 
        prof.P_Phone = txtphone.Text; 

       if(Male.Checked == true) 
       { 
        prof.P_Gender = Male.Text; 
       } 
       else if (Female.Checked == true) 
       { 
        prof.P_Gender = Female.Text; 
       } 

       if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0) 
       { 
        string fileName = FileUpload1.FileName; 
        byte[] fileByte = FileUpload1.FileBytes; 
        Binary binaryObj = new Binary(fileByte); 
        prof.P_Image = binaryObj; 
       } 
       sdc.SubmitChanges(); 
      } 

     } 

答えて

2

あなたは正規表現のバリデータを使用して試みることができる:

<asp:TextBox ID="txtfirstname" runat="server" CssClass="form-text" size="60" maxlength="128"></asp:TextBox> 
    <asp:RegularExpressionValidator ID="regexfirstName" runat="server"  
           ErrorMessage="Must be Letters" 
           ControlToValidate="txtfirstname" ForeColor="#db0d15" 
           ValidationExpression="[a-zA-Z]+"/> 

は、私は、これは英語のみの文字を使用してテストされて注意しましょう。

関連する問題