Monday, July 7, 2008

Only texts are allowed (a to z or A-Z). JS

/* Only texts are allowed (a to z or A-Z). The argument strValue is the id of the textbox.The argument required
defines the field is either optional or not. It takes the value of either true or false. The argument errorSpanID
is the id of the errorSpan
*/
function textOnly(controlID, required, errorSpanID)
{
var strValue = document.getElementById(controlID);
var errorSpan = document.getElementById(errorSpanID);
errorSpan.innerHTML = "";
var str = strValue.value;
var format = /^[A-Za-z]+$/;
if(required == true)
{
if(str == '' str == null str == "")
{
errorSpan.innerHTML = "Enter the Text";
strValue.focus();
strValue.select();
return false;
}
else
{
if (str.match(format))
{
return true;
}
else
{
errorSpan.innerHTML = "Invalid Text";
strValue.focus();
strValue.select();
return false;
}
}
}
else if(required == false)
{
if(str== '' str == null str == "")
{
return true;
}
else
{
if(str.match(format))
{
return true;
}
else
{
errorSpan.innerHTML = "Invalid Text";
strValue.focus();
strValue.select();
return false;
}
}
}
}

No comments: