Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer Recursive Prompt

    When a user clicks a Button in a Form, the JavaScript prompt dialog pops-up for the user to enter some text. The text cannot be just blank/s. Neither it should have the characters <, >, /, ?, \, |, *, ", : etc.. If the text is validated successfully, then the text is populated in a TextBox & the Form is submitted. This is how I did it:
    Code:
    <script language="JavaScript">
    function SetDirName(){
        var newdirname=prompt("Enter a name for the new folder!","New Name");
        var bln=true;
    
        for(var i=0;i<newdirname.length;i++){
            if(!(newdirname.charCodeAt(i)==32 || newdirname.charCodeAt(i)==95 || (newdirname.charCodeAt(i)>=65 && newdirname.charCodeAt(i)<=90) || (newdirname.charCodeAt(i)>=97 && newdirname.charCodeAt(i)<=122) || (newdirname.charCodeAt(i)>=48 && newdirname.charCodeAt(i)<=57))){
                bln=false;
            }
        }
    
        if(bln==false){
            alert("Invalid Characters!");
        }
    
        newdirname=newdirname.replace(/^\s*/, '').replace(/\s*$/, '')
        if(newdirname==""){
            alert("No Blanks!");
            bln=false;
        }
    
        if(bln!=false){
            if(newdirname!=null){
                document.forms[0].hdnNewDirName.value=newdirname;
                document.forms[0].submit();
            }
        }
    }
    </script>
    As such the above code works fine but what I want is if the text contains invalid characters (that have been specified above) or just blank/s, then after the alert message is shown to the user, the user should be prompted again to enter some text in the prompt dialog. In other words, the prompt dialog should pop-up as long as the text doesn't get validated successfully. If the user clicks the Cancel button on the prompt dialog, then the prompt dialog should no longer be shown to the user.

    How do I implement this in the above code?
    Last edited by arpan_de; 01-06-2007 at 09:20 PM.

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can put a while loop around the whole thing like this.

    Code:
    function SetDirName(){
        var done=false;
        
        while (!done) {
          var newdirname=prompt("Enter a name for the new folder!","New Name");
          var bln=true;
    
          for(var i=0;i<newdirname.length;i++){
              if(!(newdirname.charCodeAt(i)==32 || newdirname.charCodeAt(i)==95 || (newdirname.charCodeAt(i)>=65 && newdirname.charCodeAt(i)<=90) || (newdirname.charCodeAt(i)>=97 && newdirname.charCodeAt(i)<=122) || (newdirname.charCodeAt(i)>=48 && newdirname.charCodeAt(i)<=57))){
                  bln=false;
              }
          }
    
          if(bln==false){
              alert("Invalid Characters!");
          }
    
          newdirname=newdirname.replace(/^\s*/, '').replace(/\s*$/, '')
          if(newdirname==""){
              alert("No Blanks!");
              bln=false;
          }
          if(bln!=false){
              if(newdirname!=null){
                  document.forms[0].hdnNewDirName.value=newdirname;
                  document.forms[0].submit();
                  done = true;
              }
          }
        }
    }
    david_kw

  • #3
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, David, for the solution.

    Regards,

    Arpan


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •