Email address validation in JavaScript

This article describes an easy way of email address validation in JavaScript with the help of a regular expression. The validation function should look something like the following:

<script language="javascript">
function emailValidator(inputvalue) {
	var pattern =
		/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

	if(pattern.test(inputvalue)){
		// do something
	} else {
		alert("Email address not valid!")
	}
}
</script>

Finally, call the validation function when clicking the formular submit button.

<form name="form">Email: <input name="email" type="text">
<input type="submit" value="Submit"
	onClick="emailValidator(document.form.email.value)">
</form>

Just give it a try: emailValidator.html

Character counting in JavaScript

Character counting in JavaScript can be very useful when trying to control user input in HTML forms respectively textareas.

The following JavaScript function takes three parameters: a textarea with the message, a textbox for the remaining characters and the character number limit.

<script language="javascript">
	function charCounter(field, counter, max) {
		if (field.value.length > max)
			field.value = field.value.substring(0, max);
		else
			counter.value = max - field.value.length;
	}
</script>

The HTML part of the solution should look like the following code snippet.

<form name="textfield"><textarea name="message" cols="25" rows="6"
onKeyDown="charCounter(document.textfield.message, document.textfield.remaining, 160)"
onKeyUp="charCounter(document.textfield.message, document.textfield.remaining, 160)"></textarea>
<br>
<input type="text" name="remaining" size="3" maxlength="3" value="160" readonly>
</form>

Take a look at the example HTML page: charCounter.html

Wake On Lan in Java

The following method provides the functionality to wake up remote hosts over the network. It creates a so-called magic packet and sends it to a given broadcast address, containing the target host’s mac address.

public void wol(String bcastStr, String macStr) throws IOException {

	byte[] macBytes = new byte[6];
	byte[] magicPacket = new byte[6 + 16 * macBytes.length];

	String[] macHex = macStr.split("(\\:|\\-)");

	for (int i = 0; i < 6; i++) {
		macBytes[i] = (byte) Integer.parseInt(macHex[i], 16);
	}

	for (int i = 0; i < 6; i++) {
		magicPacket[i] = (byte) 0xff;
	}

	for (int i = 6; i < magicPacket.length; i += macBytes.length) {
		System.arraycopy(macBytes, 0, magicPacket, i, macBytes.length);
	}

	InetAddress addr = InetAddress.getByName(bcastStr);
	DatagramPacket packet = new DatagramPacket(magicPacket,	magicPacket.length, addr, 9);

	DatagramSocket socket = new DatagramSocket();

	socket.send(packet);
	socket.close();
}

Bubble Sort in Java

General

This article will show you how the bubble sort algorithm works and the way it is implemented in Java. Let’s start with an example array of five given integer values like this:

[ 12 7 5 88 42 ]

The bubble sort algorithm works by iterating over that array, comparing all pairs of integers and swapping their indices if the left one is greater than the right one. Take a look on what happens to our example array:

1st iteration
[ 12 7 5 88 42 ] swap [ 7 12 5 88 42 ]
[ 7 12 5 88 42 ] swap [ 7 5 12 88 42 ]
[ 7 5 12 88 42 ] swap [ 7 5 12 88 42 ]
[ 7 5 12 88 42 ] swap [ 7 5 12 42 88 ]

As long as there are swapping actions in an iteration, the exit condition of bubble sort is not satisfied.

2nd iteration
[ 7 5 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]

3rd iteration
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]
[ 5 7 12 42 88 ] swap [ 5 7 12 42 88 ]

And this is the sorted array:

[ 5 7 12 42 88 ]

Wuhuuu! Everything looks great. We can now take a look at the Java implementation:

Bubble Sort in Java

private int[] bubbleSort(int[] arr) {
	boolean unsorted = true;

	while (unsorted) {
		unsorted = false;
		for (int i = 0; i < arr.length - 1; i++) {
			if (arr[i] > arr[i + 1]) {
				int tmp = arr[i];
				arr[i] = arr[i + 1];
				arr[i + 1] = tmp;
				unsorted = true;
			}
		}
	}
	return arr;
}

That’s it!

I’m looking forward to describe and explain some other algorithms on binarycoded.com soon.