validate input field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realzahed
    New Member
    • Apr 2012
    • 6

    validate input field

    Hi, I want to validate the input field 'putime' in the form because even if the 'putime' input box is empty the result is calculated and nightSurcharges is also added to the cost. So I want to validate 'putime' when it is empty.
    Code:
    function TaxiFare() {
    // calculates taxi fare based upon miles travelled
    // and the hour of the day in military time (0-23).
    
    var baseFare = 14;
    var costPerMile = 7.00;
    var nightSurcharge = 20.50; // 9pm to 6am, every night //its flat 20.50 and not per mile
    
    var milesTravelled = Number(document.getElementById("miles").value) || 0;
    if ((milesTravelled < 1) || (milesTravelled > 200)) {
    alert ("You must enter 1 - 200 miles");
    document.getElementById("miles").focus();
    return false;
    }
    
    var pickupTime = Number(document.getElementById("putime").value) || 0;
    if ((pickupTime < 0) || (pickupTime > 23) || (pickupTime==null)) {  // note the hours are 0-23.  There is no 24 hour, midnight is 0 hours
    alert ("The time must be 0-23 hours");
    document.getElementById("putime").focus();
    return false;
    }
    
    var cost = baseFare + (costPerMile * milesTravelled);
    // add the nightSurcharge to the cost if it is after
    // 8pm or before 6am
    if (pickupTime >= 21 || pickupTime < 6) {
    cost += nightSurcharge;
    }
    document.getElementById("result").innerHTML = "Your taxi fare is: $. "  + cost.toFixed(2);
    }
    And here is the form
    Code:
    <form>
    Miles for Journey <input type="text" id = "miles" required><br>
    Pickup Time <input type = text id = "putime" required><br><br>
    <input type="button" value="Calculate Fare" onclick="TaxiFare()">
    <input type="reset" value="Clear"><br><br>
    <span id = "result"></span>
    </form>
  • Tippesh
    New Member
    • Apr 2012
    • 4

    #2
    Code:
    if(Textbox1.Text!="")
    {
    MessageBox.Show("Please Enter The Values");
    }
    Last edited by acoder; Apr 13 '12, 03:12 PM.

    Comment

    • realzahed
      New Member
      • Apr 2012
      • 6

      #3
      but i need to do it in javascript

      Comment

      • Tippesh
        New Member
        • Apr 2012
        • 4

        #4
        Code:
        if(document.getelementbyid(textbox id).value!="")
        {
        alert("Please Enter The Values");
        }
        If This The Corrrect Answer Plz Post Your Result
        Last edited by acoder; Apr 13 '12, 03:12 PM.

        Comment

        • realzahed
          New Member
          • Apr 2012
          • 6

          #5
          the script isn't working after i insert the above code even after i press the Calculate Fare button.
          I changed the value from textbox id to "putime"

          Comment

          • Tippesh
            New Member
            • Apr 2012
            • 4

            #6
            Dont Put the Text id Word ,Instead of give the TextBox id is there in ASP .net give that one............ .

            Comment

            • realzahed
              New Member
              • Apr 2012
              • 6

              #7
              sorry but i don't understand what u said.

              Comment

              • Tippesh
                New Member
                • Apr 2012
                • 4

                #8
                Give the TextBox id Name...........

                Comment

                • realzahed
                  New Member
                  • Apr 2012
                  • 6

                  #9
                  no it isn't validating the time field.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    All you need to do is check that putime is not empty.

                    You can add this condition to line 27, but you will need to get the actual value from the text box as you did in line 17, but without converting to a number.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by Tippesh
                      Dont Put the Text id Word ,Instead of give the TextBox id is there in ASP .net give that one
                      There's no indication that the OP is using ASP.NET. In fact, it seems certain that it isn't being used and that this isn't the issue if you read the question again.

                      Comment

                      • realzahed
                        New Member
                        • Apr 2012
                        • 6

                        #12
                        ok i got it by adding (pickupTime == "") to line 27. Thank you acoder!

                        Comment

                        Working...