Powered By Blogger

Friday, November 6, 2009

javascript for validation

function FilterNumberInput()
{

if(57<48) returnvalue="false;" keycode="="46)" returnvalue =" true;"><97)><65) returnvalue =" false;" keycode="="46||event.keyCode="="32||event.keyCode="="95)" returnvalue =" true;" keycode ="="" returnvalue =" false;" emailpat="/^(.+)@(.+)$/" 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)
{
alert("Your Email Address must be valid!")
return false
}
var user=matchArray[1]
var domain=matchArray[2]
if (user.match(userPat)==null)
{
alert("Your Email Address must be valid!")
return false;
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null)
{
for (var i=1;i<=4;i++) { if (IPArray[i]>255)
{
alert("Your Email Address must be valid!")
return false
}
}
return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null)
{
alert("Your Email Address must be valid!")

return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2>3)
{
alert("The Email Address must end in a three-letter domain, or two letter country.")
return false
}
if (len<2) regexp =" /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+="&%@!\-\/]))?/" retvalue =" inputString;" ch =" retValue.substring(0," ch ="="" retvalue =" retValue.substring(1," ch =" retValue.substring(0," ch =" retValue.substring(retValue.length-1," ch ="="" retvalue =" retValue.substring(0," ch =" retValue.substring(retValue.length-1," retvalue =" retValue.substring(0," ndecimal =" 0;" xtxt="textValue.value;" xtxt =" document.getElementById(" txtlen=" xTxt.length;" i =" 0;" x =" xTxt.substr(i," x ="="" ndecimal =" nDecimal"> 1)
{
alert("You have entered more than one decimal point!\nPlease only enter one!");
textValue.value="";
textValue.focus();
return false;
}
}
}

function IgnoreDecimal(textValue)

{
var nDecimal = 0;
var i;
var x;
var xTxt=textValue.value;
//var xTxt = document.getElementById("ctl00_ContentPlaceHolder1_txtBasicDuty").value;
var txtLen= xTxt.length;
for(i = 0; i < x =" xTxt.substr(i," x ="="" ndecimal =" nDecimal"> 0)
{
alert("You have entered decimal point!\nPlease ignore decimal point!");
textValue.value="";
textValue.focus();
return false;
}
}
}

CreditCard validation
---------------------------

public bool IsCreditCardValid(string cardNumber)
{
const string allowed = "0123456789";
int i;

StringBuilder cleanNumber = new StringBuilder();
for (i = 0; i < cardNumber.Length; i++) { if (allowed.IndexOf(cardNumber.Substring(i, 1)) >= 0)
cleanNumber.Append(cardNumber.Substring(i, 1));
}
if (cleanNumber.Length <> 16)
return false;

for (i = cleanNumber.Length + 1; i <= 16; i++) cleanNumber.Insert(0, "0"); int multiplier, digit, sum, total = 0; string number = cleanNumber.ToString(); for (i = 1; i <= 16; i++) { multiplier = 1 + (i % 2); digit = int.Parse(number.Substring(i - 1, 1)); sum = digit * multiplier; if (sum > 9)
sum -= 9;
total += sum;
}
return (total % 10 == 0);
}

--------------
public static bool IsDate(string strdt)
{
try
{
if (strdt.Trim() != "")
{
DateTime dt = Convert.ToDateTime(strdt);
}
return true;
}
catch
{
return false;
}
}


public static bool IsNumeric(string numberString)
{
try
{
if (numberString.Trim() != "")
{
double num = Convert.ToDouble(numberString);
}
return true;
}
catch
{
return false;
}
}

No comments:

Post a Comment