Monday, July 7, 2008

Validate Text JS

//Validate Text
/*Only Alpha Numeric characters are allowed. The argument txtText is the id of textbox. checkRequired means the field is necessary or not
(necessary maens true otherwise false). The argument checkLength means to check the length of the
text against the size. We must send the size if we send checkLength as true otherwise not necessary*/
function ValidateText(txtText, checkRequired, checkLength, size,spanID,controlName)
{
txtElement=document.getElementById(txtText);
var txt = txtElement.value;
var format = /^[A-Za-z0-9\s]+$/;
document.getElementById(spanID).innerHTML='';
if(checkRequired == true)
{
if(txt == "" txt == '' txt == null)
{
document.getElementById(spanID).innerHTML=controlName+' is required';
return false;
}
else
{
if(txt.match(format))
{
if(checkLength == true)
{
if(txt.length > size)
{
document.getElementById(spanID).innerHTML='Maximum is '+ size +' characters.';
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML='Invalid Characters';
return false;
}
}
}
else
{
if(txt == "" txt == '' txt == null)
{
return true;
}
else
{
if(txt.match(format))
{
if(checkLength == true)
{
if(txt.length > size)
{
document.getElementById(spanID).innerHTML= 'Maximum is '+ size +' characters.';
return false;
}
}
else
{
return true;
}
}
else
{
document.getElementById(spanID).innerHTML= 'Invalid Characters';
return false;
}
}
}
}

No comments: