Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

restrict a textbox length 1

Status
Not open for further replies.

crmpicco

Programmer
Nov 29, 2004
66
GB
is there a way to restrict the amount of characters inserted intoa textbox?

<input type="text" name="company_id" id="company_id" onChange="javascript:this.value=this.value.toUpperCase();" value="">

i only want two allowed in this one??

Picco
 
<input ... [red]maxlength="2"[/red] />

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
sorry, i meant validation for the two characters also.
 
meaning what?

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--

function restrict( obj ) {
    obj.value = obj.value.substr( 0 , 2 );
}

-->
</script>

</head>

<body>

<form name="f">
    <input type="text" name="twochars" onkeyup="restrict(this);" />
</form>

</body>

</html>

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
is it possible to have an alert box pop-ip as well? to alert the user of this problem?

thanks

picco
 
Come on now, this is elementary javascript. Some of the work has to be done on your end with a simple web search.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--

function restrict( obj ) {
    obj.value = obj.value.substr( 0 , 2 );
    alert("The box can have no more than two characters.");
}

-->
</script>

</head>

<body>

<form name="f">
    <input type="text" name="twochars" onkeyup="restrict(this);" />
</form>

</body>

</html>

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Code:
function restrict( obj ) {
    if (obj.value.length > 2) {
        obj.value = obj.value.substr( 0 , 2 );
        alert("The box can have no more than two characters.");
    }
}

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
works for me in IE and FireFox. I don't know what else to tell you.

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top