Limiting length of entered string in text box
In my JSP page I have a text box where the user enters a string. I want to send the user an alert using javascript if the length of the string is greater than 7 characters.
Does anyone know how I could go about doing this? Would something similar to what I have below work?
Thank you.
if(document.FormName.TextBoxName.length > 7 )
{
alert("Sorry, this field can not be more than 7 characters");
return false;
}
I'm assuming you are referring to the form input element <input="text" ...>. If so, you can add the attribute 'maxlength' to specify the maximum amount that the user can type, something like this:<input="text".... maxlength=7 ...>
Don't forget you'll want to check it on the JSP side as well incase your user doesn't hava JavaScript functionality.
<%
if(request.getParameter("TextBoxName").length()>7)
out.println("Error");
%>
And for JavaScript as well I believe the length is a method, string.length(), not a property string.length
so it's:
if(document.FormName.TextBoxName.value.length() > 7 )
> This may seem like a stupid question but why wouldn't
> they have the ability to run the javascript?
>
> Thank you.
Browsers such as IE and Netscape give you the capability to disable javascripting, in the event that the user is afraid of rogue javascript code that could do some damage to their system.