Try this:
<asp:TextBox ID="TextBox1" runat="server" onchange="alphaOnlyTextBox(this, 25);"></asp:TextBox>
<script type="text/javascript">
<!--
function alphaOnlyTextBox(elementRef, maxLimit)
{
var textValue = elementRef.value;
var regExObject = /.*[a-zA-Z].*/;
if ( textValue.length <= 0 )
{
alert('Please enter a value.');
elementRef.focus();
elementRef.select();
return false;
}
if ( textValue.length > maxLimit )
{
alert('Only up to ' + maxLimit + ' characters are allowed');
elementRef.focus();
elementRef.select();
return false;
}
if ( regExObject.test(textValue) )
{
return true;
}
else
{
alert('Only characters a - z, or A - Z are allowed');
elementRef.focus();
elementRef.select();
return false;
}
return true;
}
// -->
</script>
Note that I attached the event handler to the element in the HTML
declaration. This will work, but will produce a compiler warning. The
proper way to do this for a server control is in the Page_Load handler
of the server-side code like this:
TextBox1.Attributes.Add("onchange", "alphaOnlyTextBox(this, 25);");