Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prompt() require answer 1

Status
Not open for further replies.

xsw1971

Programmer
Jun 21, 2001
153
0
0
US
I have a prompt that requires an answer from the user. However some users have figured out that typing a space or two will get them past the prompt. How do I check for non-space characters? I don't simply want to say if(x == ' ') because they might have more than 1 space.

Thanks!
 
utilize regular expressions. take this example:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]

<html>
<head>
<title>Untitled</title>
<script type="text/javascript"><!--
var re = /\S+/;
alert(re.test('  cory'));
alert(re.test('cory'));
alert(re.test('  '));
alert(re.test('12 '));
//--></script>
</head>

<body>

</body>
</html>

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
explanation: \S matches any non-whitespace.

so, you could do something like:

Code:
var re = /\S+/;
var str = prompt("enter value");

while( !re.test( str ) )
    str = prompt("enter value");

alert('Finally!');

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Of course, if people want to get past the prompt after this technique is employed, they'll just enter a period or a 'g' or anything. What is it this prompt is actually for?

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Thanks for fast reply, cLFlaVA! It works great, except for one thing: the user can get around it now by clicking Cancel. Here's the code, I'm sure there's a better way of doing this:
Code:
function cancel_reason() {
  var re = /\S+/;
  var reason = prompt("Please provide a reason for canceling the request","");

// This prevents the user from clicking cancel - they MUST enter something 
  if(reason) {
    while( !re.test(reason) )
    var reason = prompt("Please provide a reason for canceling the request","");
    // additional processing goes here; here is where it breaks down if the user clicks Cancel

  // if user clicks Cancel intially, it will return them to this function
  } else {
   cancel_reason();
}

Any thoughts?
 
well, you're calling the cancel_reason() function even if the user clicks cancel. you're also re-asking for a reason if the user doesn't enter a correct value.

get rid of that else statement.

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
But that's exactly what I want it to do. I don't want the user to be able to click Cancel - they HAVE to give a response, and since there's no way of removing that cancel button (that I'm aware of) I have to go this way. Here is what I ended up doing, and I used all the alerts so I could see where the script was executing (they won't stay there). Works perfectly now. Thanks for your help with the reg expression.
Code:
function cancel_reason() {
  var re = /\S+/;
  var reason = prompt("Please provide a reason for canceling the request","");

  if(reason) {
    alert("Answer provided");
    if(!re.test(reason)) {
      alert("You only provided spaces");
      cancel_reason();
    } else {
      alert("Good answer");
    }
  } else {
    alert("No answer provided");
    cancel_reason();
  }
}
 
Hi,
Of course,the users could just close their browser...






[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Actually they can't (at least I can't) unless they kill the process. But that defeats the whole process anyway because they lose all the changes they made on the form - it's a project management tool, and they wouldn't want to have to re-do everything.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top