简体   繁体   中英

Textbox validation and other controls

Well i'm pretty new in c# classes and WPF programming.

I make often forms with several textfields and i was wondering if it is possible to make a class to validate the textboxes and other input controls.

Well if i put it in a class i don't have to repeat the code over and over again.

So i validate names on empty and if they are valid text values. An e-mail address is also checked if it is valid and number fields are checked if no letters are written.

Is it possible to do this in a class. Because after the validation you have to return the value.

Some help would be nice.

Take a look at the Validation class: http://msdn.microsoft.com/en-us/library/System.Windows.Controls.Validation(v=vs.110).aspx

You can reuse the validation rules across various textboxes, or you could create some custom controls with those validation rules built in (eg an EMailAddressTextBox, NumericTextBox, etc.)


Heading

<tr>
        <td class="auto-style11" style="color: #FFFFFF">
            Địa chỉ email:</td>
        <td class="style21">
            <asp:TextBox ID="txt_dcMail" runat="server" Width="140px"></asp:TextBox>
        </td>
        <td class="style15">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="txt_dcMail" ErrorMessage="(7)" ForeColor="Red"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_dcMail" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">(Invalid Email![enter image description here][1])</asp:RegularExpressionValidator>
        </td>
    </tr>

You can use the same code as Tuan Ngo has provided above.

Or you can also create a separate javascript page for validation.

function validation() {

var chkTextbox = document.getElementById("textBox1").value; 
var alertMsg = "";

if(chkTextbox==null){
   alertMsg = alertMsg.length > 0 ? alertMsg + "Enter text!":"Enter text!"; 
   textBox1.style.backgroundcolor = "yellow";
   textBox1.focus();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM