Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tnsmart

macrumors 6502
Original poster
Aug 23, 2008
279
24
I have been trying to figure out how to set a maximum number of characters that can be entered into a prompt box that appears as part of a javascript, activated by clicking a button on a PHP page. I've found lots of information on how to display an alert when too many characters are entered, but I want it to work like the maxlength attribute does for a textfield in HTML.

This is the code that I have right now:
Code:
function user()
  {
  var username=prompt("Please enter your name.","");
  if (username==null || username=="" || username==" ")
    {
    document.getElementById('message').innerHTML="Username invalid!";
    }
  else
    {
    return username;
    }
  }

Any help is appreciated. Thanks.
 
As mentioned the standard prompt window has no attributes to be attributed to it for setting a max length. You could implement your own prompt box though and use a standard input tag with the maxlength attribute set. Visitors can override these attributes anyways though so don't rely on it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.