Max Length validation is generally we use like we dont allow user to enter the anything once the limit is over but most of the time we left with one loop hole in that.
Restrict the user on the keypress but we don't restrict the user on paste and if user paste something he/she can cross the limit event because at that time the key press event doesn't gets fired. So below is the solution for the same.
C# code
txtComments_Demotion.Attributes.Add("onkeypress", "java-script:maxLength('" + txtComments_Demotion.ClientID + "','92');");
txtComments_Demotion.Attributes.Add("onpaste", "java-script:maxLengthPaste('" + txtComments_Demotion.ClientID + "','92');");
Javascript function in aspx page.
function maxLength(fieldName,maxChars)
{
field=document.getElementById(fieldName);
if(field.value.length >= maxChars) {
event.returnValue=false;
return false;
}
}
function maxLengthPaste(fieldName,maxChars)
{
field=document.getElementById(fieldName);
event.returnValue=false;
if((field.value.length + window.clipboardData.getData("Text").length) > maxChars) {
return false;
}
event.returnValue=true;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment