Tuesday, November 5, 2013

JavaScript to validate Price input into a TextBox

Guys, first time in my life I am writing JavaScript for one of my project. I need a client side validation for an input into asp:TextBox.
I wrote a JavaScript function that would, actually, take whole element as object and use a regex to match the input with a pattern.
I learned that we don’t need to create an object of a RegEx() as we do in C#.
A “var” variable has “match” function that will use regex pattern to match the input.
Below is the javascript function that I wrote to validate my input.

<script language="javascript">
    function validatePrice(textBoxId) {
        var textVal = textBoxId.value;
        var regex = /^(\$|)([1-9]\d{0,2}(\,\d{3})*|([1-9]\d*))(\.\d{2})?$/;
        var passed = textVal.match(regex);
        if (passed == null) {
            alert("Enter price only. For example: 523.36 or $523.36");
            textBoxId.Value = "";
        }
    }
</script>


I wanted to fire the validation after I lost a focus on text box. To achieve this I did Google and went through all Form, Window etc type of event described at http://www.w3schools.com/tags/ref_eventattributes.asp . I used “onblur” event which is as same as OnLostFocus() in most of the WinForm controls.

<asp:TextBox ID="TextBox19" runat="server" Visible="False" Width="183px"
      onblur="javascript:return validatePrice(this);"></asp:TextBox>


This is my first javascript function. There may be best chance to improvise it. Please post your comments with changes.
Thanks & Enjoy!

Tuesday, October 29, 2013

Inserting an element into a heap

Inserting an element into a heap     
In this article we examine the idea laying in the foundation of the heap data structure. We call it sifting, but you also may meet another terms, like "trickle", "heapify", "bubble" or "percolate".
Insertion algorithm
Now, let us phrase general algorithm to insert a new element into a heap.
  1. Add a new element to the end of an array;
  2. Sift up the new element, while heap property is broken. Sifting is done as following: compare node's value with parent's value. If they are in wrong order, swap them.
Example
Insert -2 into a following heap:
Insert a new element to the end of the array:
In the general case, after insertion, heap property near the new node is broken:
To restore heap property, algorithm sifts up the new element, by swapping it with its parent:
Now heap property is broken at the root node:
Keep sifting:
Heap property is fulfilled, sifting is over.
Source heap
After -2 insertion
Complexity analysis
Complexity of the insertion operation is O(h), where h is heap's height. Taking into account completeness of the tree, O(h) = O(log n), where n is number of elements in a heap.
Code snippets
Java implementation
public class BinaryMinHeap {     
public void insert(int value) {
            if (heapSize == data.length)
                  throw new HeapException("Heap's underlying storage is overflow");
            else {
                  heapSize++;
                  data[heapSize - 1] = value;
                  siftUp(heapSize - 1);
            }
      }    

     
     
private void siftUp(int nodeIndex) {
            int parentIndex, tmp;
            if (nodeIndex != 0) {
                  parentIndex = getParentIndex(nodeIndex);
                  if (data[parentIndex] > data[nodeIndex]) {
                        tmp = data[parentIndex];
                        data[parentIndex] = data[nodeIndex];
                        data[nodeIndex] = tmp;
                        siftUp(parentIndex);
                  }
            }
      }
}
C++ implementation
void BinaryMinHeap::siftUp(int nodeIndex) {
      int parentIndex, tmp;
      if (nodeIndex != 0) {
            parentIndex = getParentIndex(nodeIndex);
            if (data[parentIndex] > data[nodeIndex]) {
                  tmp = data[parentIndex];
                  data[parentIndex] = data[nodeIndex];
                  data[nodeIndex] = tmp;
                  siftUp(parentIndex);
            }
      }
}

void BinaryMinHeap::insert(int value) {
      if (heapSize == arraySize)
            throw string("Heap's underlying storage is overflow");
      else {
            heapSize++;
            data[heapSize - 1] = value;
            siftUp(heapSize - 1);
      }
}