Nigel Whitworth's Development Site

You are currently not logged in.

ASP.NET Validation

ASP.NET has built in fuctions to add validation to your text boxes so you don't have to write the code yourself. Below are two examples, the first validates that the user has entered a valid email address using the regular expression validator. The second will ensre that the user has entered a value and not left it blank. There are other validators which I will describe at a later date.

    <ASP:TextBox id=TextBox1 runat=server />

    <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
        ControlToValidate="TextBox1" ValidationExpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$"
        Display="Static"
        Font-Name="verdana" Font-Size="10pt"> Please enter a valid e-mail address
    </asp:RegularExpressionValidator>


    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
        ControlToValidate="TextBox1" Display="Dynamic"
        Font-Name="Verdana" Font-Size="10pt" > *
    </asp:RequiredFieldValidator>


The validator code is run at the server so the user will need to submit the page before any error messages are displayed. The text between the two validator tags will be what is displayed on screen if what the user has entered is invalid. If the text box has more than one validator, it will validate the top requirement and will only check the user matches the second validation if the first passes. It won't carry out both validations at the same time. If there is code run by submit button, you could add code to check that the page is valid and display an additional message by checking the Page.IsValid property.


If you intend to vote or make a comment, please enter the security code.

Make a comment

*Comments are the views of individuals, they may or may not be correct.
All comments are reviewed and accepted or rejected.
If you give an email address, you will be sent an email when someone makes a comment on this page.*

Name :
Email :
Comments :


Only name and Code is compulsory.
 
 
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)