A Good Regex For Creating Usernames

A regular expression (called regex) is a way for a programmer to instruct how a program should look for a specified pattern in text and then what it should do when each pattern match is found. Rather than going through each character of a string and doing matches, regex makes life easier for programmers to do search and matches. This sample regex is a good pattern for use in creating usernames.

The regex below means that only alphanumeric (only lowercase letters) are allowed including an underscore and a dot.

^[a-z0-9_.]$

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(1 votes, average: 5.00 out of 5)
 Loading ...

ASP.NET: ValidationSummary For A CreateUserWizard Control

ASP.NET provides a control that uses the Membership API to create users on the fly via the CreateUserWizard control. The only drawback with this control is that you cannot include any other details like first name, last name, address and others. You would have to place all other details as a separate page, called a Wizard Step. When you use forms, naturally, you would have to use validation in order to have the correct data that will be stored in your database.

ASP.NET also has a nifty control called ValidationSummary that collects all errors of controls found in the tag and displays them as either a List or a Bulleted List. I typically got stuck for half a day looking for a solution because my ValidationSummary control does not display when it is in the CreateUserStep of my wizard. You can use a property called ValidationGroup to specify which set of controls will be collected by the ValidationSummary control for display. You do this by giving the ValidationGroup a name, which is case-sensitive by the way. My problem persisted because the ValidationSummary control for my CreateUserStep has a different name. I later found out that you would have to use the CreateUserWizard’s ID as the ValidationSummary’s ValidationGroup’s name for this to work. See the sample code below.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="myfile.aspx.cs" Inherits="myfile" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Add User</title>
</head>
<body>
 
<form id="form" runat="server">   
 
<asp:ValidationSummary runat="server" ID="InfoVS" 
  ShowSummary="true" DisplayMode="BulletList" ValidationGroup="PersonalVG" 
  HeaderText="<b>Please review the following errors:</b>" 
/>
<asp:ValidationSummary runat="server" ID="AddUserVS" 
  ShowSummary="true" DisplayMode="BulletList" ValidationGroup="AddUserWizard" 
  HeaderText="<b>Please review the following errors:</b>" 
/>
<br />
<asp:CreateUserWizard ID="AddUserWizard" runat="server" BorderStyle="ridge" BackColor="aquamarine"> 
    <TitleTextStyle Font-Bold="True" Font-Names="Verdana" /> 
    <StartNavigationTemplate>
        <asp:Button ID="StartNextButton" runat="server" CausesValidation="true" CommandName="MoveNext" Text="Next" ValidationGroup="PersonalVG"/>
    </StartNavigationTemplate>
	<WizardSteps> 
        <asp:WizardStep ID="PersonalInfoStep" AllowReturn="true">
            <table>
                <tr>
                    <td style="width: 213px; height: 1px">first name</td>
                    <td style="width: 58px; height: 1px"></td>
                    <td style="width: 417px; height: 1px">
                        <asp:TextBox ID="firstname" runat="server" Width="237px"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ID="FirstnameRequiredFieldValidator" ValidationGroup="PersonalVG" ControlToValidate="firstname" ErrorMessage="Firstname is required."/>
                    </td>
                </tr>
            </table>
        </asp:WizardStep>
        <asp:CreateUserWizardStep ID="CreateUserStep" runat="server"> 
            <ContentTemplate>
               <table style="width: 525px; height: 33px;" id="table">
                    <tr>
                        <td colspan="3" style="width: 213px; height: 10px">
                            <asp:Label ID="label" runat="server"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 213px; height: 10px;">username</td>
                        <td style="width: 58px; height: 10px"></td>
                        <td style="width: 417px; height: 10px">
                            <asp:TextBox ID="username" runat="server" Width="199px"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="UsernameRequiredFieldValidator" ValidationGroup="AddUserWizard" ControlToValidate="username" ErrorMessage="Username is required."/>
                         </td>
                    </tr>
                    <tr>
                        <td style="width: 213px">password</td>
                        <td style="width: 58px"></td>
                        <td style="width: 417px">
                            <asp:TextBox ID="password" runat="server" Width="199px" TextMode="Password"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="PasswordRequiredFieldValidator" ValidationGroup="AddUserWizard" ControlToValidate="password" ErrorMessage="Password is required."/>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 213px; height: 1px;">confirm password</td>
                        <td style="width: 58px; height: 1px"></td>
                        <td style="width: 417px; height: 1px;">
                            <asp:TextBox ID="cpassword" runat="server" Width="199px" TextMode="Password"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="ConfirmPasswordRequiredFieldValidator" ValidationGroup="AddUserWizard" ControlToValidate="cpassword" ErrorMessage="Confirm password is required."/>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 213px; height: 1px">email</td>
                        <td style="width: 58px; height: 1px"></td>
                        <td style="width: 417px; height: 1px">
                            <asp:TextBox ID="email" runat="server" Width="234px"></asp:TextBox>
                            <asp:RequiredFieldValidator runat="server" ID="EmailRequiredFieldValidator" ValidationGroup="AddUserWizard" ControlToValidate="email" ErrorMessage="Email is required."/>
                        </td>
                    </tr>  
                </table>
            </ContentTemplate> 
        </asp:CreateUserWizardStep>
        <asp:CompleteWizardStep ID="CompleteWizardStep" runat="server"> 
            <ContentTemplate> 
                Your account has been successfully created.</td> 
                <asp:Button ID="ContinueButton" CommandName="Continue" runat="server" Text="Continue" /> 
            </ContentTemplate> 
        </asp:CompleteWizardStep> 
    </WizardSteps> 
</asp:CreateUserWizard> 
 
</form>
 
</body>
</html>

I specifically added a StartNavigationTemplate tag and overrode the asp:Button and its properties because the validation does not work if you do not set the button’s CausesValidation property to true. Hence, your ValidationSummary control would show nothing since no validation took place. Notice that my ValidationSummary’s ValidationGroup is called PersonalVG. You can name it any way you want but to use the ValidationSummary’s ValidationGroup for the CreateUserStep, you would have to use the CreateUserWizard’s ID for this to work. Otherwise, no validation error messages will be displayed and you will just be stuck on the CreateUserStep page.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

ASP.NET: Get Access Of Control Within A CreateUserWizard

Getting access to a control from within c# is straightforward. You just call the ID name and call the method or property that you want to use. If you are going to get access to a control from within a CreateUserWizard control and your code may look something like

<asp:CreateUserWizard ID="RegisterUserWizard" runat="server" BorderStyle="ridge" BackColor="aquamarine"> 
    <TitleTextStyle Font-Bold="True" Font-Names="Verdana" /> 
    <WizardSteps> 
        <asp:CreateUserWizardStep ID="CreateUserStep" runat="server"> 
            <ContentTemplate>
                     <asp:Label ID="label" runat="server"></asp:Label>
            </ContentTemplate>
        </asp:CompleteWizardStep> 
    </WizardSteps> 
</asp:CreateUserWizard>

you call the Label control by

Label lbl = (Label) RegisterUserWizard.CreateUserStep.ContentTemplate.label;

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

ASP.NET: GridView Control

One of the reasons why I’m impressed with asp.net’s rich set of controls is its GridView control. This control displays your data in a table structure complete with delete, update and select features. It even has paging in case you have lots of records in your database. The best thing about this paging is that everything is done for you, the links, the paging number, the paging labels and others. If you click a page link, it will retrieve the next set of record(s) for you without changing any SQL query. For example, you can use this SQL query to retrieve all records of the table.

select * from [table_name];

In MySQL, you would need to supply the LIMIT keyword in order to get a specific set of rows. With GridView’s paging feature enabled, everything is done for you. Neat huh. Took me like a day in getting this one to work though because I had problems with the update and delete function. Since I am using MySQL, turns out when you do parameters in your query commands, the @ sign does not work because it is only for MS SQL. You would have to change the @ to ?.

Take this sample asp.net code for a GridView control as an example. This assumes you have a working database connection to MySQL. If you do not know how to connect to a MySQL in asp.net, go here to use the custom MySQLProvider classes by Rakotomalala Andriniaina.

<asp:SqlDataSource ID="srcUsers" runat="server" 
	ProviderName="MySql.Data.MySqlClient"
	ConnectionString="<%$ ConnectionStrings:db %>" 
	SelectCommand="select pkid, username, email, creationdate from users" 
	DeleteCommand="delete from users where pkid = ?pkid"
	UpdateCommand="update users set email = ?email where pkid = ?pkid"
>
</asp:SqlDataSource> 
 
<asp:GridView ID="UsersGridView" runat="server" DataSourceID="srcUsers" AllowSorting="true"
			  DataKeyNames="pkid" AllowPaging="true" PageSize="10"
			  AutoGenerateColumns="false"> 
	<Columns> 
		<asp:BoundField DataField="pkid" HeaderText="pkid" Visible="false" /> 
		<asp:BoundField DataField="username" HeaderText="Username" ReadOnly="true" SortExpression="username"/> 
		<asp:BoundField DataField="Email" HeaderText="Email" /> 
		<asp:BoundField DataField="CreationDate" HeaderText="Creation Date" DataFormatString="{0:F}" readonly="true" SortExpression="creationdate"/> 
		<asp:CommandField ShowEditButton="true" />
		<asp:CommandField ShowDeleteButton="true" />
	</Columns>
 
	<PagerSettings Mode="NextPrevious" PreviousPageText="< Previous" NextPageText="Next >" /> 
	<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
</asp:GridView>

If you read e-books regarding asp.net, the queries used are for MS SQL. It’s been a long time since I dabbled with MS SQL, so when I read about it, I thought the @ sign for parameters were required for any database used. Took me some time to know that ? has to be used. See the DeleteCommand and UpdateCommand properties of the SqlDataSource tag.

How you design your GridView control is up to you. With styles, you can override how the table would look like. The other GridView like control that I had used before that looks like asp.net’s version is the datagrid taglib for Java. The downside with using the taglib is that if your SQL query returns lots of rows, it may not load fast and may use up resources in doing so because it gets all records at one time. Asp.net’s GridView control totally impressed me in more ways.

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...