开发者

Limiting number of form characters with Javascript validation

How can I limit the number of characters that an input form allows? I'm using a validation like this

//Last name
var开发者_开发问答 x=document.forms["regForm"]["lname"].value
if (x==null || x=="")
  {
  alert("Last name must be filled out");
  return false;
  }


Why not set the maxlength attribute (maxLength property) on the <input> element?


you can do something like this:

<script>

function testInput(obj, max_length){
    if(obj.value.length > max_length){
        alert(obj.name + "'s length is too long");
    }
}

</script>

<input name='lName' onchange='testInput(this, 8);'/>
<input name='fName' onchange='testInput(this, 15);'/>

fiddle: http://jsfiddle.net/maniator/FQTM5/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜