...

View Full Version : Recursive Prompt



arpan_de
01-06-2007, 09:08 PM
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:

<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?

david_kw
01-07-2007, 12:26 AM
You can put a while loop around the whole thing like this.



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

arpan_de
01-11-2007, 03:19 AM
Thanks, David, for the solution.

Regards,

Arpan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum