Use switch statement to deal with form input value : Switch « Statement « JavaScript Tutorial






<html>
    <head>
      <title>Using the switch statement</title>
    <script language="JavaScript">
    <!--
    function verifyDay(form){
      var myEntry = form.day.value.toUpperCase();

      var firstPart = "You have entered a day at the first of the week";

      var endPart = "You have entered a day at the end of the week";

      var weekEnd = "You have entered a weekend day";

      switch(myEntry){
        case "MONDAY" :
          alert(firstPart);
          break;
        case "TUESDAY" :
          alert(firstPart);
          break;
        case "WEDNESDAY" :
          alert('You have entered a "hump" day');
          break;
        case "THURSDAY" :
          alert(endPart);
          break;
        case "FRIDAY" :
          alert(endPart);
          break;
        case "SATURDAY" :
          alert(weekEnd);
           break;
        case "SUNDAY" :
          alert(weekEnd);
          break;
        default :
          alert('You have entered an invalid day');
      }
    }
    -->
    </script>
    </head>
    <body>
    <form name="myForm">
    <b>Please enter a day of the week:</b><br>
      <input type=TEXT value="" name="day">
      <input type=BUTTON value="Verify" name="myButton" onClick='verifyDay(this.form)'>
    </form>
    </body>
    </html>








3.5.Switch
3.5.1.Using the switch Structure
3.5.2.Use switch statement to deal with form input value
3.5.3.Switch statement with return value from a prompt dialog
3.5.4.Switch statement with default value
3.5.5.Switch with boolean value
3.5.6.Switch Statement with Integer as the control variable
3.5.7.Switch Statement with calculation