2016-04-02 25 views
1

Metin kutusunda dize olup olmadığını kontrol etmek istiyorum veya içinde bir metin olup olmadığını kontrol etmek istiyorum. Bu dize değil hata mesajı görüntüledim ancak bu kodu denedim ancak metin kutusuna hata mesajı girdiğimde ekranmetin kutusunun dize içerip içermediğini kontrol edin

html kodu: kod arkasında

<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(); 
      } 

     } 

cevap

2

Sen regex doğrulayıcı kullanarak deneyebilirsiniz:

<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]+"/> 

Bu sadece İngilizce karakterler ile test edilmiştir dikkat edeceğiz.

İlgili konular