0

Please can someone help me with a code doing a switch statement with a form to check something like:

<script language="JavaScript">
<!--
function verifyPerf(form){
  var myEntry = form.number(); //check the edit box number, didn't know how?! 
  var firstPart = "1. You need to do thing 1";
  var endPart = "2. You need to to do thing 2";

 switch(myEntry){
    case "<= 3000" :
      alert(firstPart);
      break;
    case ">3000 and <9000" :
      alert(endPart);
      break;
   //would like to add 3 more cases 
    default :
      alert('You have entered an invalid performance number');
  }
}
-->
</script>
</head>
<body>
<form name="myForm">
<b>Please enter your performance number:</b><br>
  <input type=number value="" name="perfNumber">
  <input type=BUTTON value="Verify" name="myButton" onClick='verifyPerf(this.form)'>
</form>
</body>
</html>

thanks for your help. Lx

There is alos this part that's not working. function verifyPerf(form){ var myEntry = form.value;

..... and the form:

Please enter the Performance:

  • This isn't what `switch` is designed for, it's what `if () {} else if () {} /*etc*/ else {}` is designed for. – Paul S. Mar 04 '14 at 21:45

1 Answers1

1

Your switch statement is looking for the string '<= 3000' or '>3000 and <9000' not a number.

Check this out: Switch on ranges of integers in JavaScript

This demonstrates how to properly switch against a range of numbers.

Community
  • 1
  • 1
daddygames
  • 1,880
  • 1
  • 11
  • 18
  • 2
    Perhaps your answer could include some code rather than only linking to a duplicate question – Paul S. Mar 04 '14 at 21:46
  • @PaulS.: please no. Just gain enough reputation for flagging. This is a misuse of the duplicate handling system. I do not understand the "deemed invalid" decisions for the flags, but it is likely because there are robo-reviewers who saw one upvote, and they thought it would mean it is useful, but it is not following the site policies. – László Papp Mar 05 '14 at 02:07
  • 1
    I apologize for not following procedure for identifying the duplicate question. I was unaware of the flagging process. – daddygames Mar 05 '14 at 13:51