Maximum length for javascript prompt box

Discussion in 'Web Design and Development' started by tnsmart, Dec 30, 2010.

  1. tnsmart macrumors 6502

    Aug 23, 2008
    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:
    function user()
      var username=prompt("Please enter your name.","");
      if (username==null || username=="" || username==" ")
        document.getElementById('message').innerHTML="Username invalid!";
        return username;
    Any help is appreciated. Thanks.
  2. bootedbear macrumors 6502


    Sep 13, 2004
    Austin, TX
    There is no way to do that with the standard prompt box.

    Perhaps there's a jQuery prompt plugin you could use?
  3. angelwatt Moderator emeritus


    Aug 16, 2005
    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.

Share This Page