Undefined string input

John Ertle Jr 1 Reputation point
2022-03-26T02:04:57.56+00:00

The ampm = -5 from Textbox2. It should be a string of "AM" or "PM".
the value for hr24 is undefined. It cannot be greater than 12 or less than zero.

function Button1_Click(){
var hr = Textbox1.value;
var ampm = Selectbox1.value;
Textbox2.value = ampm;
if ((ampm == "AM") && (parseInt(hr) == 12)){
var hr24 = 0;
}
if ((ampm == "AM") && (parseInt(hr) < 12)){
var hr24 = hr;
}
if ((ampm == "PM") && (parseInt(hr) < 12)){
var hr24 = parseInt(hr) + 12;
}
if ((ampm == "PM") && (parseInt(hr) == 12)){
var hr24 = 12;
}
Textbox4.value = hr24;

}

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. John Ertle Jr 1 Reputation point
    2022-03-26T02:10:24.327+00:00

    Here is the html.
    <select id="Selectbox1" class="sel">
    <option value="PM">PM</option>
    <option value="AM">AM</option>
    </select>

    0 comments No comments

  2. Sudipta Chakraborty - MSFT 1,106 Reputation points Microsoft Employee
    2022-03-26T04:43:53.807+00:00

    @John Ertle Jr :

    The code provided by you has the scope of variable "hr24" local to the If block therefore when you are moving out of the if block and using the variable you are getting the following error "value for hr24 is undefined". You can update the code for Button1_Click() function to fix your issue. Please find below the updated code:

    function Button1_Click(){  
    var hr = Textbox1.value;  
    var ampm = Selectbox1.value;  
    Textbox2.value = ampm;  
    
    var hr24 = 0;  
      
    if ((ampm == "AM") && (parseInt(hr) == 12)){  
       hr24 = 0;  
    }  
    if ((ampm == "AM") && (parseInt(hr) < 12)){  
       hr24 = hr;  
    }  
    if ((ampm == "PM") && (parseInt(hr) < 12)){  
      hr24 = parseInt(hr) + 12;  
    }  
    if ((ampm == "PM") && (parseInt(hr) == 12)){  
      hr24 = 12;  
    }  
    Textbox4.value = hr24;  
      
    }  
    
    0 comments No comments

  3. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2022-03-28T06:11:59.103+00:00

    Hi @John Ertle Jr ,
    The value of hr24 is indeterminate only when hr>12. because you didn't define it.
    You said you want hr24 not to be greater than 12 or less than zero, the range of values for hr24 depends on hr.
    You can refer to the code below:

    <body>  
        <form id="form1" runat="server">  
            <div>  
                <select id="Selectbox1" class="sel">  
                    <option value="PM">PM</option>  
                    <option value="AM">AM</option>  
      
                </select><br />  
                <asp:Label ID="Label4" runat="server" Text="Label"> AM: hr >=12 && hr <= 0;  
                PM: hr >=-12 && hr <= 0</asp:Label><br />             
                <asp:Label ID="Label1" runat="server" Text="hr"></asp:Label>  
                <asp:TextBox ID="Textbox1" runat="server"></asp:TextBox><br />  
                <asp:Label ID="Label2" runat="server" Text="ampm"></asp:Label>  
                <asp:TextBox ID="Textbox2" runat="server"></asp:TextBox><br />  
                <asp:Label ID="Label3" runat="server" Text="hr24"></asp:Label>  
                <asp:TextBox ID="Textbox4" runat="server"></asp:TextBox><br />  
                <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="Button1_Click()"/>  
            </div>  
        </form>  
    </body>  
    </html>  
    
        function Button1_Click() {  
            var hr = Textbox1.value;  
            var ampm = Selectbox1.value;  
            Textbox2.value = ampm;  
            var hr24 = "";  
            if ((ampm == "AM") && (parseInt(hr) == 12)) {  
                 hr24 = 0;  
            }  
            if ((ampm == "AM") && (parseInt(hr) < 12) && (parseInt(hr) >= 0) ){  
                 hr24 = hr;  
            }  
            if ((ampm == "PM") && (parseInt(hr) <= 0) && (parseInt(hr) >= -12)) {  
                 hr24 = parseInt(hr) + 12;  
            }  
            if ((ampm == "PM") && (parseInt(hr) == 12)) {  
                hr24 = 12;  
            }  
            if (hr24 == "" && hr24 != 0) {  
                hr24 = "invalid value"  
            }      
            Textbox4.value = hr24;  
        }  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments