Javascript Date Compare

I needed to validate if an end date was greater than or equal to a start date. To do this I was using:

var elemFromVal = $('#elemFromVal').val();
var elemToVal = $('#elemToVal').val();
return elemFromVal <= elemToVal;

This was working up until the point the years where different. For example “12/1/2010” to “12/2/2010” would work but not “12/1/2010” to “1/1/2011”.

To fix this issue I needed to create a new date object and compare those instead:

var elemFromVal = $('#elemFromVal').val();
var elemToVal = $('#elemToVal').val();

var d1=elemFromVal.split('/');
var d2=elemToVal.split('/');

var startDate=new Date(d1[2],(d1[1]-1),d1[0]);
var endDate=new Date(d2[2],(d2[1]-1),d2[0]);

return startDate <= endDate;

Bulk Total DNS Updates w/ GoDaddy

I currently have a customer who is switching hundreds of websites over to us for hosting on a dedicated server. They currently use GoDaddy’s Total DNS and it works out pretty good for them.

While trying to find a way to switch all IP addresses without having to do them one by one I did a little research for bulk Total DNS updates. I quickly ran into it can’t be done. I could only export a DNS set and import it one by one. So basically we where stuck manually updating each domain manually.

Then I figured out I was looking for the wrong “term”. I needed to use the “Copy” function.

Here’s how to do it:

  1. Log into the Domain Manager
  2. Edit a domain you want changed to the new IP
  3. Select Total DNS Control
  4. Check the DNS entries you want duplicated
  5. Click the Copy button at the top
  6. Select the domains to copy the records to
  7. Click “Copy Records”

My customer’s life just got a whole lot easier!

Incrementing Dynamic HTML Element Attributes

On occasion I clone DOM elements to repeat form sections dynamically. During this I need to increment the name and id attributes so I can reference them later on.

For example: name=”txtField1″ becomes “txtField2”

To make this happen I use a little but of slick jQuery magic. This increments the id and name attributes and empties the value for input, select, and textarea elements.

var newNum = new Number(num + 1);
var newElem = $('.clonedInput:last').clone(false).attr('id', 'input' + newNum);
newElem.find('input,select,textarea')
    .attr('name', function() {
        return $(this).attr('name').replace(/^(.+?)(\d+)$/, function(s, p1) {
            return p1 + newNum;
        });
    })
    .attr('id', function() {
        return $(this).attr('id').replace(/^(.+?)(\d+)$/, function(s, p1) {
            return p1 + newNum;
        });
    })
    .val('');

I started out using the regex:

^(.+)(\d+)$

But that failed after 10. The .+ expression needs to be non-greedy using:

^(.+?)(\d+)$

Here’s another example using a table and an button to create a new row:

$("#productChildren").delegate("#btnAddAdditionalProductChildren", "click", function(event) {
	var $clone = $("#productChildren > tr").last().clone(false);
	$(this).remove();
	var $inputs = $( $clone ).find("input[type='text']");
	$($inputs).each(function() {
		var newNameAttr = $(this).attr('name').replace(/\d+$/, function(match, p1) {
			return parseInt(match) + 1;
		});	
		$(this).attr('name', newNameAttr);
		$(this).val('');	
	});
	$($clone).appendTo("#productChildren");
});

How Technology Saved My Dinner

I went out for dinner last night and had some Grouper and Crab meat for dinner, it was pretty amazing. Sitting in the patio next to a lake with a small water fall and fountain. It was a perfect evening, with the sun slowly going down and about 72 degrees, hardly any wind. Then my phone buzzed… “Thunder storm warning issued”. I thought this can’t be right – not a dark cloud in the sky. So I pulled up “The Weather Channel” app on my droid and started the Radar loop. Ooh – dark red heading right for us and fast. Reading the alert – 60MPH wind gusts moving at 45MPH. So I kindly asked the waitress if we could move indoors because there was a nasty storm coming. She replied – “Really? Well you’re smarter than the average chicken”. So they moved us inside and we continued with dinner. Ten minutes later everyone is scrambling inside with the staff trying to hold down the fort – including condiments, umbrellas, and seat covers. Guess they where just “your average chicken”. “I love my droid”

LCD TV’s These Days

Today an article on cnet news caught my eye: Get a 40-inch HDTV for $399.

I thought wow! After reading the specs it’s a 1080p w/ 2,000:1 contrast ratio w/ an average 4.5/5 review rating. I bought my 37″ Toshiba 720p LCD TV about two years ago for $750. So I can basically get a TV that is twice as good for a little more than half the cost. Not a bad deal. Even Amazon can’t touch this price that Walmart has. They list it for another $150.

However I do have to give Toshiba props for excellent customer service regarding a warranty issue I had. They replaced pretty much the whole guts of my TV at no cost, on site, and with awesome communication. This TV in the article is a Proscan brand. I wonder how they would handle a warranty claim? Would there be the trade-off?

It’s just always amazing how fast electronics drop in price. Just recently I remember speaking with my grandfather on how he bought two 5MB hard drives back in the day for a cool $3,500 – thinking he would never need more space than that. Good luck finding 5MB anything these days.

non-breaking spaces and jQuery.trim()

When performing a .trim() function on a string pulled via the .text() tag in jQuery, &nbsp; will convert into a special space that will not be removed.

Example:

<div id="div1">  This is text  </div>

<script type="text/javascript">
jQuery(document).ready(function() {
trimString = jQuery.trim($('#div1').text());
</script>

I was able to catch the Unicode character, which ended up being 160, by using:

alert(trimString.charCodeAt(0));

After performing a regex replace on the string, I was able to achieve a truly trimmed string.

<script type="text/javascript">
$(document).ready(function() {
trimString = $('#div1').text();
var re = new RegExp(String.fromCharCode(160), "g");
trimString = jQuery.trim(trimString.replace(re, " "));
});
</script>

Commit Monitor for SVN

I use SVN on a regular basis to commit and update code either myself or my team is currently working on. However one issue I’ve had, that I’ve never really looked into, was notification of commits so that each programmer can be notified automatically of changes. I’ve heard of some scripts that can be created to email the proper programmers, but today I found a nice tool that makes this painless. By default this program, that hides away in your system tray for Windows, checks for a new commit every 90 minutes. When a new commit is found, it displays a small window showing an update, similiar to goodsystray for Google Wave. This small program can monitor multiple projects and there a few options you can customize such as running a script after a new commit is found or changing how often it checks for commits. The program is called CommitMonitor and can be found at http://tools.tortoisesvn.net/CommitMonitor.

SQL Management Studio 2008 Error: Saving changes is not permitted

Recently I came accross a blockade after upgrading my SQL Management Studio from 2005 to 2008. All of a sudden if I added a “no-null” option column in a table or inserted a column in the middle of the existing schema I could not save. At first I thought there was some hidden permission I was missing, but after futher research I find out it’s a setting in the Studio itself.

The full error is: Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created.

To get rid of this annoying “feature”, take these steps.

  1. Go to Tools > Options > Designers > Table and Database Designers via the menu
  2. Uncheck the option ‘Prevent saving changes that require table re-creation

Note: This is a safety feature and turning this off opens you up to possible loss of data, so make sure you know what you’re modifying or at least make a backup.

Alternate System Recovery

On HP Laptops, sometimes F11 will get you into the recovery partition. However if this fails and you can still get into a runnable Windows you have an option. Run compmgmt.msc (Computer Management) in Windows and select Disk Management. Then mark the recovery partition (usually D) active by right clicking on the drive’s icon. Then restart. Don’t press any keys and if the partition is still good it should boot up the recovery partition so you can get your computer back to factory condition. This was verified on a HP Pavilion dv6000 series laptop.

Duplicate Errors Using jquery.validate on Dynamic Forms

I have a form that clones a div upon clicking an add button. For validation I use the jquery.validate plugin. When the add button is clicked, I exectute the following to clear the errors.

validator.resetForm();

However, if inline errors are displayed before this being called, and then they are displayed again after the form is reset, the inline errors are displayed one time on the first div, two times on the second div, and so-forth. The alleviate this issue I’m now destroying the error element.

validator.resetForm();
$('label.error').remove();