Maximum length for javascript prompt box

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

  1. tnsmart macrumors 6502

    Joined:
    Aug 23, 2008
    #1
    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.
     
  2. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #2
    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

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    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