March 2007 - Posts - Developers Anonymous

March 2007 - Posts

So now that I have my ADSL up and running, I have decided to run off a 1gig unshaped service from www.webafrica.co.za and so far so good. That was until last night when I hosted a Gears of War server on xbox live for a few local guys. To cut a long story short, after 2 and half hours of play I had used 285MB of bandwidth.

So this morning I started looking for something that could save me money because there's no way my cap is going to last at this rate. Now a 1gig prepaid is R125 for unshaped bandwidth but 10gig local bandwidth is only R70. BUT to connect xbox live I need international bandwidth. There is a solution! You can split your local and international bandwidth using this amazing little programme called Route Sentry. Now I can log onto xbox live using my international bandwidth but when I host / play a local match then it automatically uses my local bandwidth.

Unforunately I cannot seem to find what the programmers name is but to you ant1body, I am eternally grateful! Check out his site here for more tools that he has written!

I learnt something very interesting and useful today. If you want to see which websites link to yours, simply go to www.google.com and in the search criteria enter link:http://www.yourAddressHere.com and hit search. It works quite well.

My ADSL was recently activated and I buy my bandwidth on a prepaid basis. Webafrica seriously rocks but I saw that www.i-junction.co.za is cheaper by R15. Now I know its only R15 but hey, I like my money so the less I hand out the better right? Well apparently www.i-junction.co.za don't want my business but after 4 attempts I gave us. I just kept getting an error!

Which brings me to my point, how can anyone run an "online store" without regularly checking that its working?!?!?! It is infuriating and there is no way I'll be going back to them for business! WebAfrica FTW!

A few months back the CNA website started advertising the Playstation 3. Now, I'm not against the PS3 but I am fully against the normal salesman bullsh*t and propaganda. So I complained to the ASA (Advertising Standards Authority) along with my friend CraigN, and two blokes from the forums and we won. CNA were forced to withdrawn the claims in their advert. In the process Ster Kinekor also sent a reply to the ASA to substantiate their claims but nowhere in the documentation were those claims to be found!

Although all the sony fanboi's out there are calling me stupid and such, all I ask is that you think about it. It was more the principle of the advert than the product. I'm sure the PS3 will be a kickass console for all the rich boys out there.

http://www.asasa.org.za/ResultDetail.aspx?Ruling=3617 à ASA Ruling

http://www.mybroadband.co.za/nephp/?m=show&id=5985 à MyADSL article

http://mybroadband.co.za/vb/showthread.php?t=70264 à Discussion

Using regular expressions in ASP.Net is even easier than implementing them in plain html and yet few developers are using them. In this article I will show two ways of implementing Regular Expressions, using a Regular Expression Validator and using plain Javascript in ASP.Net. Both examples are very very simple. We will validate a telephone number and an email address.

How to Implement Regular Expressions in ASP.Net with Validator Controls.

1) Create a new a page in a web application project in visual studio.
2) Lets create a basic table with space for a Telephone Number, Email Address and submit button. Use the following code if you wish:
 <table>
  <tr>
   <td>
    Telephone Number:
   </td>
   <td>
    <asp:TextBox runat="server" ID="txtTelNumber" />&nbsp;
   </td>
   <tr>
   
   </tr>
  </tr>
  <tr>
   <td>
    Email Address:
   </td>
   <td>
    <asp:TextBox runat="server" ID="txtEmail" />&nbsp;
   </td>
   <tr>
   
   </tr>
  </tr>
  <tr>
   <td colspan="2" style="text-align:right">
    <asp:Button runat="Server" ID="btnSubmit" Text="Submit" />
   </td>
  </tr>
 </table>
 
3) Then, in the empty cells add RegularExpressionValidator Controls from your toolbox.
4) Give each of your validators a descriptive ID and specify the control that validator should check.

Now, for our telephone number validator we are going use the same expression that I used in my preveious post. Under the controls Properties, set the ValidationExpression = ^(\()?(011|012)(\)|-)?([0-9]{3})?([0-9]{4}|[0-9]{4})$

For the email validator, click on the (...)button in the ValidationExpression Box. A popup box will be presented, in this box you can enter your own custom validation OR select a predefined regEx. In this case select the Internet Email Address.

Do yourself a favour and enter your own custom error messages for both validators.

Now run the page and test it. The moment the user clicks the submit button it validates both fields and if the fields comply with their respective regular expressions then the postback will proceed HOWEVER, should either NOT comply with their regular expression the postback will not occur and the error message will be shown. Once a compliant Telephone Number and/or email address is entered, the error message will disappear and the user can submit the form again.

Here is the whole ASP.Net Code for the above example:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <table>
                <tr>
                    <td>
                        Telephone Number:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtTelNumber" />&nbsp;
                    </td>
                    <td>
                        <asp:RegularExpressionValidator
                            ID="regExTelephone" runat="server" ControlToValidate="txtTelNumber" Display="Dynamic"
                            ErrorMessage="Please enter a valid Telephone Number" ValidationExpression="^(\()?(011|012)(\)|-)?([0-9]{3})?([0-9]{4}|[0-9]{4})$"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td>
                        Email Address:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtEmail" />&nbsp;
                    </td>
                    <td>
                        <asp:RegularExpressionValidator
                            ID="regExEmail" runat="server" ControlToValidate="txtEmail" Display="Dynamic"
                            ErrorMessage="Please enter a valid Email Address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:right">
                        <asp:Button runat="Server" ID="btnSubmit" Text="Submit" />
                    </td>
                </tr>
            </table>
    </div>
    </form>
</body>
</html>

 


How to Implement Regular Expressions in ASP.Net using BLOCKED SCRIPT

1) Create a new a page in a web application project in visual studio.
2) Lets create a basic table with space for an Email Address and submit button. Use the following code if you wish:
 <table>
  <tr>
   <td>
    Email Address:
   </td>
   <td>
    <asp:TextBox runat="server" ID="txtEmail" />&nbsp;
   </td>
  </tr>
  <tr>
   <td colspan="2" style="text-align:right">
    <asp:Button runat="Server" ID="btnSubmit" Text="Submit" />
   </td>
  </tr>
 </table>
 
3) In the your header tags create a script block and write the neccessary functions to check the email entry. You can use the following script:

<script type="text/javascript">
    function ValidateEmail()
    {
      //Declare regular expression
      var re = new RegExp('^.+@[^\.].*\.[a-z]{2,}$');

      if (window.document.getElementById('txtEmail').value.match(re))
      {
        //allow postback to continue
        return true;
      }
      else
      {
        //inform user that the Telephone Number is not valid
        alert("Email not valid");
       
        //Set the focus on non-compliant field
        window.document.getElementById('txtEmail').focus();
       
        //Prevent the postback
        return false;
      }
    }
</script>

4) Then simply add the following attribute to your submit button:
 OnClientClick="BLOCKED SCRIPTreturn ValidateEmail();"
 

Here is the full code for this page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
    <script type="text/javascript">
    function ValidateEmail()
    {
      //Declare regular expression
      var re = new RegExp('^.+@[^\.].*\.[a-z]{2,}$');

      if (window.document.getElementById('txtEmail').value.match(re))
      {
        //allow postback to continue
        return true;
      }
      else
      {
        //inform user that the Telephone Number is not valid
        alert("Email not valid");
       
        //Set the focus on non-compliant field
        window.document.getElementById('txtEmail').focus();
       
        //Prevent the postback
        return false;
      }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <table>
                <tr>
                    <td>
                        Email Address:
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="txtEmail" />&nbsp;
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:right">
                        <asp:Button runat="Server" ID="btnSubmit" OnClientClick="BLOCKED SCRIPTreturn ValidateEmail();" Text="Submit" />
                    </td>
                </tr>
            </table>
    </div>
    </form>
</body>
</html>

 


And there we go. Now nobody who has read can say they don't know how to implement Regular expressions. I realised that this isn't the most complete guide to regular expressions but Rome wasn't built in a day. This is aimed at those people who have never in their lives implemented a Regular Expression.

I would appreciate any feedback and/or suggestions you may have on this or even requests pertaining to Regular Expressions.


 

kick it on DotNetKicks.com

Recently Brian Johnson posted something that made me think. There are hundreds and probably thousands of web developers out there who have never used regular expressions and even if the knew about them they don't know HOW to implement them. Regular expressions are really awesome once you start using them. They are exceptionally powerful although they look really difficult in the beginning. For the beginners I suggest that you visit a site like http://www.regular-expressions.info/ as they have many many examples and a very good tutorial. Once you have some idea of whats cooking then bookmark http://regexlib.com/CheatSheet.aspx. Between these two sites you'll be writing your own regular expressions in no time. But then again, why re-invent the wheel? Visit http://regexlib.net/ for a whole range regular expressions!

 So what is a regular expression? It is a set of rules that a certain set of text must adhere to. For example, if we want to test whether a user has entered a valid Johannesburg or a Pretoria telephone number. Most developers first test whether the number contains 011 or 012 as the area code. Then if that was successful test whether the user entered 7 other digits. This would usually comprise of a few "if" statements and hopelessly too much code. Whereas with a regular expression if can be done with 1 line of code.

 I am going to write this example using only Javascript and tomorrow I will post an entry using ASP.Net.

<script type='text/javascript'>function ValidateNumber()
{
  //Declare regular expression
  var re = new RegExp('^(\()?(011|012)(\)|-)?([0-9]{3})?([0-9]{4}|[0-9]{4})$');

  if (window.document.getElementById('txtTelephoneNumber').value.match(re))
  {
    alert("Valid telephone Number");
  }
  else
  {
    alert("Telephone Number not valid");
  }
}
</script>

 and thats it! One line and it checks whether the entered number is in the following format: 0128134456 or 0118176543. Isn't that so much easier? You can use regualr expressions to check the validity of telelphone numbers, email addresses, URI's, date/times and anything else. Creating your own complex regular expression's may take a while to master but it'll save countless hours of redudant coding.

 Here's the full code for a quick test page. Just paste it in notepad and save the file as a .html file and open with Internet Explorer.

 <html>
<head>
<script type='text/javascript'>
function ValidateNumber()
{
  //Declare regular expression
  var telRegEx = new RegExp('^(\()?(011|012)(\)|-)?([0-9]{3})?([0-9]{4}|[0-9]{4})$');

  //Match textbox value to Regular Expression
  //Match method returns a boolean value
  if (window.document.getElementById('txtTelephoneNumber').value.match(telRegEx))
  {
    alert("Valid telephone Number");
  }
  else
  {
    alert("Telephone Number not valid");
  }
}
</script>
<head>
<body>
<span>Enter a telephone number: </span><input type='text' id='txtTelephoneNumber' /><br /><br />
<input type='button' onclick="ValidateNumber();" value="Test Number" />
</body>
</html>

 I firmly believe that any web developer should be using regular expressions for client side input validation. It'll save you time and improve your application's performace since you won't need to do your validations client side. This is something that is exceptionally easy and easy to implement.  The .Net implementation is even easier but I'll post that tomorrow tonight!

 You can read Brian's post about Regular Expressions here: Do Regular Expressions scare developers?

kick it on DotNetKicks.com

Blogging has become quite a passtime for many people but us developers tend to use it for a more technical purpose. Writing small articles and tutorials to hopefully gain a little bit of respect from our peers and mostly to evangelis whatever set of tools or technologies we are using.

 But what about internal tools? Many large developement houses have their own set of inhouse tools but you cannot blog about that online. So a good friend of mine, Andre (http://mailowl.co.za/) came up with the following idea. Business Blogging. It is actually a great idea that needs more attention, thus I am writing this post. Read his post here @ http://mailowl.co.za/wordpress/?p=113 as it covers his whole idea in detail. It is an idea i think might really catch on in the bigger companies.

 Good luck with the idea Andre and may it be really successful!

kick it on DotNetKicks.com

Well ScottGu's latest update on ORCAS is now available and it makes for incredibly interesting reading! Extension Methods and the new System.LinQ namespace are discussed.

Read his article here

Virtual Earth Map Control SDK now available is now available for download here. This can be used in your windows forms applications as well as ASP.Net Websites. Virtual Earth 3D development manager Duncan Lawler has an article in the MSDN Magazine about the techinques his team has used in developing and implementing this new control. You can read all about it here.

 For those developers out there familiar with the control, here is a list of new features with this release.


kick it on DotNetKicks.com

http://www.fring.com is a startup company similar to the local mxit but instead of offering texting services they offer VOiP services. It integrates with GTalk, Skype, MSN and their own custom service. I tested it out quick with my mate over GTalk and it worked very nicely.

 Its a free service but I'm not sure for how long as its in BETA phase. At the moment it looks like it has only been developed for Nokia phones but there is an "other" option. Had a chat to my bud for a minute or two and if only took about 15c off my account.

 MTN subscribers beware, MTN might nail you hectically on that because its a VOiP call. I saw article somewhere about it and I cannot remember if this *has* been implemented or whether its *going* to be implemented.

 

Enjoy!

The March CTP of Orcas has been released and its looking good and very intersting! Download here
 
Some of the small changes to the language are quite useful and something I see as neccesary. If there's one thing I truely hate, its being redundant. An example of this is when creating a new class. Why declare an internal variable, a property and constructor? Its a mission that isn't really needed. In this new release of Orcas they have address the above mentioned situation. So now it'll happen as follows.
 
public class Car
{
        public string Make()
        {
              get; set;
        }
}
 
so as you can see there's no longer the dilemma of internal variables and the debate on whether to assign values to the internal varible or the variables property.
 
Declaring a new instance of the class will work as follows:
 
Car newCarInstance = new Car{Make="Hyundai"};
 
this will create a new instance of the Car class with the Make Property set to Hyundai already. The same has been done for Generic Lists. I'll post more as soon as I find tonight and/or over the weekend.
 


kick it on DotNetKicks.com