prompt window value to alert window problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cruzzer
    New Member
    • Apr 2010
    • 1

    prompt window value to alert window problem

    I need to know how can I exclude a data item from the Alert Window’s list message if the user leaves a prompt window blank.

    Example :
    If the user types a value to both “idnumber” and “departmen t” prompt but leaves the “access” prompt empty or clicks cancel, I want the Alert Message List to look…

    LIKE THIS:
    ID Number: “value”
    Department: “value”

    and …

    NOT LIKE THIS
    ID Number:”value”
    Access Code: null
    Department: “value”

    NOR LIKE THIS:
    ID Number: “value”
    Access Code: undefined
    Department: “value”


    Code:
    <head>
    <script type="text/javascript">
    
    function Information() {
    	idnumber = window.prompt("Type your ID Number", "");	
    	access = window.prompt("Type your Access Code", "");
    	department = window.prompt("Type your Department", "");
    	
    window.alert( "ID Number: " + idnumber + '\n' +"Access Code: " + access + '\n' +"Department: " + department + '\n' ); }	
    </script>
    </head>
    
    
    
    <body>
    <script type="text/javascript">
    
    information();
    
    </script>
    </body>
    </html>
    Last edited by Dormilich; Apr 29 '10, 05:39 AM. Reason: please use [code] [/code] tags when posting code
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    a simple if-statement like in the following example would do the job:

    Code:
    function Information() {
        var idnumber = window.prompt("Type your ID Number", "");    
        var access = window.prompt("Type your Access Code", "");
        var department = window.prompt("Type your Department", "");
        
        var message = '';
    
        if ( idnumber != null ) {
            message += 'ID number: ' + idnumber;
        }
    
        window.alert( message );
    }
    so if you cancel the idnumber-prompt then the alert should be empty.

    kind regards

    Comment

    Working...