Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

In case you will be working on a page that needs to validate the first character of the TextBox entered by a user then here are two options that you can use:

Option 1: Using an array

 

   1:  <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
   2:  <script type="text/javascript">
   3:      function CheckFirstChar(o) {
   4:          var arr = ['A', 'B', 'C', 'D'];
   5:          if (o.value.length > 0) {
   6:              for (var i = 0; i < arr.length; i++) {
   7:                  if (o.value.charAt(0) == arr[i]) {
   8:                      alert('Valid');
   9:                      return true;
  10:                  }
  11:                  else {
  12:                      alert('InValid');
  13:                      return false;
  14:                  }
  15:              }
  16:          }
  17:      }
  18:  </script>
  19:  </asp:Content>
  20:  <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  21:  <asp:TextBox ID="TextBox1" runat="server" onblur="return CheckFirstChar(this);"></asp:TextBox>
  22:  </asp:Content>

 

The example above uses an array of string for storing the list of  characters that a TextBox value should start with. We then iterate to the array and compare the first character of TextBox value to see if it matches any characters from the array.

Option 2: Using Regular Expression (Preferred way)

 

   1:  <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
   2:  <script type="text/javascript">
   3:      function CheckFirstChar(o) {
   4:          pattern = /^(A|B|C|D)/;
   5:          if (!pattern.test(o.value)) {
   6:              alert('InValid');
   7:              return false;
   8:          } else {
   9:              alert('Valid');
  10:              return true;
  11:          }
  12:      }
  13:  </script>
  14:  </asp:Content>
  15:  <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  16:  <asp:TextBox ID="TextBox1" runat="server" onblur="return CheckFirstChar(this);"></asp:TextBox>
  17:  </asp:Content>

 

The example above uses regular expression with the pattern  /^(A|B|C|D)/. This will check if the TextBox value starts with A,B,C or D. Please note that it's case sensitive. If you want to allow lower case then you can alter the patter to this /^(A|B|C|D)/i. The i in the last part will cause a case-insensitive search.

 

That's it! I hope someone find this post useful!

Posted on Wednesday, February 2, 2011 12:32 AM ASP.NET , JavaScript | Back to top


Comments on this post: Validate if aTextBox Value Start with a Specific Letter

# re: Validate if aTextBox Value Start with a Specific Letter
Requesting Gravatar...
The training kit is very helpful. It contains some basic information regarding Visual Studio 11. The software is indeed advanced and many are using this because of its practicality and advanced features. - Marla Ahlgrimm
Left by Marla Ahlgrimm on Feb 21, 2012 7:57 AM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net