codehutch.com

Online Code Repository

Email Format Checker

Here’s a simple javascript function that checks if a given string is a valid email address.

function email_checker(email){
if(email == “”){
return false;
}
else{
if(email.match(/b(^(S+@).+((.com)|(.net)|(.edu)|(.mil)|(.gov)|(.org)|(..{2,2}))$)b/gi)){
return true;
}
else{
return false;
}
}
}

Try it:

test



Tags:

A holiday can either be fix or floating. Fix Holidays are those that occur on the same day each year, like Christmas. Floating Holidays on the other hand are those which may occur on different days in different years. An example would be Martin Luther King, Jr. Day which is celebrated in US every “3rd Monday of January”.

Now, what I need is a function that determines whether a given date is a holiday or not. I have a table called tbHolidays with the following columns:

HolidayIDX (int)
HolidayDate (datetime)
HolidayDayMonth (varchar(10)) - just to ease myself from having to parse day and month out of date everytime I query, I added this column and always made sure that it should be of form mm/dd
HolidayType (varchar(10)) - Fix or Floating

This table stores all the holidays defined by the user. Determining fix holidays are pretty straight-forward, where you just have to compare the dates. But floating holidays need a little trick. To make it clean and organized, I created two “scalar-valued” db functions. These are:

fn_DayOfOccurence - Returns the week number of the given date. Used in fn_IsHoliday.
fn_IsHoliday - Returns holidayID if a given date is a holiday, if not it returns Null.

fn_IsHoliday


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

CREATE Function [dbo].[fn_IsHoliday] (@DATE datetime)

RETURNS int
BEGIN
DECLARE @holiday int;

– make a query first on Fix types. Negate the year part.

Select top(1) @holiday = HolidayIDX from tbHolidays where HolidayDayMonth = SUBSTRING(convert(varchar(30),@DATE,101),1,5) and HolidayType = ‘Fix’

If @holiday > 0
Return(@holiday)
Else
Begin

– if given date is not found on Fix types, query on Floating
– all we have to do is compare day-of-occurence (e.g 3), day-of-week (e.g Monday) and month-name (e.g January)
– Basically, we’re just comparing the ‘important’ parts of a date in floating-holiday-format (e.g 3rd Monday of January)

select top(1) @holiday = HolidayIDX from tbHolidays where datename(mm, HolidayDate) =  datename(mm, @DATE) and datename(dw, HolidayDate) = datename(dw, @DATE) and dbo.fn_DayOfOccurence(HolidayDate) = dbo.fn_DayOfOccurence(@DATE) and HolidayType = ‘Floating’

If @holiday > 0
Return(@holiday)
End
Return(null)
END

fn_DayOfOccurence

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

CREATE Function [dbo].[fn_DayOfOccurence] (@DATE datetime)

RETURNS int
BEGIN
declare @dayNum as int
declare @dow as int

set @dow = 1
Select @dayNum = DATEPART(dd, @DATE)
while(@dayNum > 7)
begin
set @dow = @dow + 1
set @dayNum = @dayNum - 7
end

Return(@dow);
END

How to Use:

select fn_IsHoliday(@givenDate)

– If not Null then it’s holiday, otherwise it’s not

These functions may not be perfect, but I hope they could at least give an idea to someone out there who might need them.



Tags: