Tuesday, July 31, 2007

Hi,
Most of the time we require to validate the control in the client side using javascript.Below are some useful function


For removing blank spaces with regular Expression-----------------------

function trimAll(value)
{
var temp = value;
var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
if (obj.test(temp))
{
temp = temp.replace(obj, '$2');
}
var obj = / +/g;
temp = temp.replace(obj, " ");
if (temp == " ")
{
temp = "";
}
return temp;
}

For Zip code validation US Zip Code------------------------------------------

function isValidZipCode(val)
{
var exp=/(^\d{5}$)(^\d{5}-\d{4}$)/;
var re = new RegExp(exp);
return (val.match(re));
}

For checking Alphanumeric value-----------------------------------
function CheckAlphanumeric (e)
{
var key;
key = e.which ? e.which : e.keyCode;
if((key>=48 &&amp;amp; key<=57)(key>=65 && key<=91) (key >=97 && key<=123)) {
e.returnValue= true;
}
else
{
alert("please enter Alphanumeric only");
e.returnValue = false;
}
}
Time in 12 hour foramt-----------------------------------------------

function validateTime(strValue,state)
{
var objRegExp = /^([1-9]1[0-1]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
if(!(objRegExp.test( strValue )))
{
alert("Please Enter "+ state +" time in 12 hour format eg 11:30 ");
}
return objRegExp.test( strValue );
}

Hope this will help all
Regards
Pankaj

0 comments:

Post a Comment