PPD PPD - 4 years ago 101
jQuery Question

Not able to restrict user to enter more than 8 characters

Hi following is my loginform in that I want to restrict user from entering 8 characters in userName field but it is not working following is my code:

<div data-role="content">

<form id="loginForm">
<div data-role="fieldcontain" class="ui-field-contain ui-body ui-br">
<label for="username">Username:</label> <input type="text"
name="username" id="username" value="" maxlength="8" /> <label for="password">Password:</label>
<input type="password" name="password" id="password" value="" /> <label
for="dob">Date of birth:</label> <input type="password"
type="password" name="dob" id="dob" value="" />
<div >
<fieldset class="ui-grid-a" >
<div class="ui-block-a" >
<input type="submit" data-role="button" value="Login"
<div class="ui-block-b">
<input type="reset" data-role="button" value="Cancel"



<script type="text/javascript">


<script type="text/javascript">
$("#username").live('keydown', function (event)

if ((event.keyCode.length>8))



But still user can enter any number of characters in UserName field. what's wrong in this code? Any suggestion will be appreciated. Thanks in advance

Answer Source

Add a maxlength to the input field.. instead of using script to restrict.

<input type="text" name="username" id="username" value=""  maxlength="8" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download