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;

}

[469 byte] By [moined_mogul] at [2007-9-26 2:33:01]
# 1
please help...thank you
moined_mogul at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Please let me know if you need more info to help?
moined_mogul at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
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 ...>
hungyee at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Otherwise you would wantif(document.FormName.TextBoxName.value.length > 7 )the alternative would not be measuring what the user actually typed.
Breakfast at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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 )

bryanwclark at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
This may seem like a stupid question but why wouldn't they have the ability to run the javascript?Thank you.
CHEERS at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

hungyee at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
I was caught out by the String.length()/String.length thing in Javascript a couple of days ago. I thought it was a method, but it is definitely a property. http://developer.netscape.com/docs/manuals/js/client/jsref/string.htm#1193437
Breakfast at 2007-6-29 9:55:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...