Thursday, January 7, 2010

Validating Date Using JavaScript.

Date Should not be Character.
-------------------------------

function IsChar(sText) {
var ValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. ";
var IsNumber = true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++) { Char = sText.charAt(i); if (ValidChars.indexOf(Char) == -1) { IsNumber = false; } } return IsNumber; } Date Should Not Contains Special Characters. ---------------------------------------- function checkSpecialchar(objName) { var checkOK = "!@#$%^&*()_-+|\<>?.";
var checkStr = objName;

var allValid = true;
var allNum = "";
var ch;

for (i = 0; i < checkStr.length; i++) { ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break; } //if (ch != ",") //allNum += ch; } if (!allValid) { return (false); } else { return (true); } } Now we Start validating date. Here is the code for that. ------------------------------------------------ var startDate = document.forms[0].txtStartDate.value; var validDate = startDate.indexOf('/'); var fIndex = startDate.charAt(2); var lIndex = startDate.charAt(5); if (fIndex != '/') { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (lIndex != '/') { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (validDate == -1) { alert("Please Use (Slash) /to Enter Date"); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else if (CheckSpecialChar(startDate)) { alert("Special Characters are not allowed."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else if (IsChar(startDate)) { alert("Date Should be Numeric."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } else { var txtDate = startDate.split('/'); var day = txtDate[0]; var month = txtDate[1]; var year = txtDate[2]; var dt = new Date(); var curYear = dt.getFullYear(); var curMonth = dt.getMonth() + 1; if (year < curYear) { alert("Year Should be Greater Than or Equal to Current Year."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } if (year == curYear) { if (month < curMonth) { alert("Month Should be greater than current month."); document.forms[0].txtStartDate.focus(); document.forms[0].txtStartDate.select(); return false; } } if (month > 12) {
alert("Please Enter date in DD/MM/YYYY Format.");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day > 31) {
alert("Day Should be less than or equal to 31 for the given month.");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}
if (month == 2 && (year % 4 == 0 || year % 400 == 0)) {
if (day > 29) {
alert("Leap Year February day should be less than or equal to 29");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}
if (month == 2 && (year % 4 != 0 || year % 400 != 0)) {
if (day > 28) {
alert("Not a Leap Year February day should be less than or equal to 28");
document.forms[0].txtStartDate.focus();
document.forms[0].txtStartDate.select();
return false;
}
}

=============================================================
Enjoy....

Validating Email Using JavaScript

=============================================
function isEmail(emailStr) {
var emailPat = /^(.+)@(.+)$/
var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars = "\[^\\s" + specialChars + "\]"
var quotedUser = "(\"[^\"]*\")"
var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom = validChars + '+'
var word = "(" + atom + "|" + quotedUser + ")"
var userPat = new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$")

var matchArray = emailStr.match(emailPat)

if (matchArray == null) {
return false
}

var user = matchArray[1]
var domain = matchArray[2]


if (user.match(userPat) == null) {
return false
}

var IPArray = domain.match(ipDomainPat)

if (IPArray != null) {
for (var i = 1; i <= 4; i++) { if (IPArray[i] > 255) {
return false
}
}
return true
}

var domainArray = domain.match(domainPat)

if (domainArray == null) {
return false
}

var atomPat = new RegExp(atom, "g")
var domArr = domain.match(atomPat)
var len = domArr.length

if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 3) {
return false
}

if (len < 2) {
var errStr = "Email ID is missing a Host Name!"
return false
}

return true;
}
=============================================

Setting Maxlength Of Textarea Using JavaScript

==================================

function SetMaxLength(Object, MaxLen) {
return (Object.value.length < MaxLen);
}
==================================
Here Object is the Id of Textarea and MaxLen is the maximum no of characters allowed.