How to validate Date using Javascript Regular Expression




Here is a simple Javascript function to validate a date in US format.

function isDateValid(date) {
	//RegEx to validate date
	/*
	* ^[0-1][1-2] : The month starts with a 0 or 1 followed by either a 1 or a 2
	* [- / ]?: Followed by  an optional "-" or "/".
	* (0[1-9]|[12][0-9]|3[01]) : The day part must be either between 01-09, or 10-29 or 30-31.
	* [- / ]?: Day part will be followed by  an optional "-" or "/".
	* (18|19|20|21)\\d{2}$ : Year begins with either 18, 19, 20 or 21 and ends with two digits.
	*/
	var dateRegExPattern = "^[0-1][1-2][- / ]?(0[1-9]|[12][0-9]|3[01])[- /]?(18|19|20|21)\\d{2}$";
	if(date.match(dateRegExPattern)){
		return true;
	}else{
		return false;
	}
	
}


How to redirect homepage in Joomla




Joomla is a wonderful platform to publish websites. With readily available templates you can have your website up and running in no time. Sometimes though you may need to direct your visitors to a different page than your regular home page, for instance you may want to direct all the visitors to a promotion page during special sales events. There is a very simple and easy way of doing this in Joomla. Continue Reading →

Sort numbers in Java to find minimum and maximum values without using Array



Recently a reader contacted me with a question about sorting numbers in Java. After sorting the number the program then needs to print the largest and smallest values. I have written a post earlier that shows one way of finding largest and smallest numbers. That approach used Arrays but the reader wanted to find largest and smallest values from a group of numbers without using Arrays. So here is another approach.

This program accepts input from the user and then prints out the largest and smallest numbers.






import java.util.*; 
public class NumberSorter{
	public static void main(String args[]){
	  double a, b, c, x, y;
	  Scanner console = new Scanner(System.in); 
	  System.out.print("Enter the first number: " ); 
	  a = console.nextDouble() ;
	  System.out.print("Enter the second number: " ); 
	  b  = console.nextDouble() ;
	  System.out.print("Enter the third number: " ); 
	  c = console.nextDouble();
	  x= Math.min(a, Math.min(b, c));
	  y= Math.max(a, Math.max(b, c));
	  System.out.println("\n" +x + " is the smallest number.\n" + y 
	                     +" is the largest number.");
	}
}

Hope you find this useful. Share your ideas on how else can we find largest and smallest values from a group of numbers Java.

Java String comparison. The difference between == and equals().

javaString


Many Java beginners find it difficult to differentiate between == operator and the “equals()” method when comparing String variables in Java. They assume that both operations perform the same function and either one can be used to compare two string variables. I have even seen many experienced programmers committing the mistake of using “==” to compare the values of two strings.

So what is the difference between “==” and “equals()” and what is the correct method of comparing two string variables?
Continue Reading →

An HTML element you don't want to omit.

Various Doctypes

Various Doctypes

Doctypes

As web developers we have the tendency to blame web browsers for inconsistencies and for not displaying the pages as designed. Most of the time the blame is justified, but there are instances when the blame lies at the feet of the web developers. For example, incorrect CSS declarations, missing closing tags in HTML documents will break you website. Another common and often overlooked reason of broken web pages is the missing or incorrect DOCYTYPE declaration. Continue Reading →

How to remember CSS Shorthand properties


Using margin or padding properties of CSS in the following syntax is straight forward.


{
 padding-top:5px;
 padding-right:12px;
 padding-bottom: 15px;
 padding-left: 20px;
}

But the shorthand version of these properties may be a little difficult to remember for CSS beginners.
The above CSS declaration can be written as;


{padding:5px 12px 15px 20px;}

Now how do you know which one is which? The answer to this is simple. First value (5px in this example) applies to top and then you move clockwise, 2nd applies to right, the 3rd applies to bottom and the 4th value applies to left.

Why you should stop using onload method.

Waiting for onload
Onload

Onload

It is amazing that many web developers still use onload method embedded in the BODY tag, like


 <HEAD>
 <BODY onload="document.contact.userID.focus();">
  <form name="contact">
   <input type="text" name="userID" >
  </form>
  </BODY>
 </HEAD>

There are two problems with this line of code.

  1. Embedding code (behavior) with HTML (structure) makes it difficult to maintain the page.
  2. onload() method will executes only after the page is fully displayed.
  3. Let’s see how to fix these problems. Continue Reading →

Five free tools to choose perfect colors for your website.

Rainbow colors

Rainbow colors


Color coordination plays a big role in the look and feel of a website. An appealing color scheme is an important prerequisite for eye catching web design. Dull or very sharp colors or mismatch colors will not only look ugly it may overshadow other nice elements of your website. Choosing vibrant, harmonious colors that are pleasant to eyes enhance the aesthetics appeal of a website. Following is a list of online resources you can use to construct the color schemes for your next web design project.
Continue Reading →

How to position HTML element in the center using CSS.

Positioning text in the center of an element is easily done using text-align:center property. But how do you position an entire element, for e.g; a div or an image in the center of the containing parent element?

For example, if you want to put an image at the center of your header div how do you accomplish this?

There are several ways to align an element in the center but the preferred and elegant method is to use margin property. Continue Reading →