By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,702 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,702 IT Pros & Developers. It's quick & easy.

validate input field

P: 6
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. function TaxiFare() {
  3. // calculates taxi fare based upon miles travelled
  4. // and the hour of the day in military time (0-23).
  5.  
  6. var baseFare = 14;
  7. var costPerMile = 7.00;
  8. var nightSurcharge = 20.50; // 9pm to 6am, every night //its flat 20.50 and not per mile
  9.  
  10. var milesTravelled = Number(document.getElementById("miles").value) || 0;
  11. if ((milesTravelled < 1) || (milesTravelled > 200)) {
  12. alert ("You must enter 1 - 200 miles");
  13. document.getElementById("miles").focus();
  14. return false;
  15. }
  16.  
  17. var pickupTime = Number(document.getElementById("putime").value) || 0;
  18. if ((pickupTime < 0) || (pickupTime > 23) || (pickupTime==null)) {  // note the hours are 0-23.  There is no 24 hour, midnight is 0 hours
  19. alert ("The time must be 0-23 hours");
  20. document.getElementById("putime").focus();
  21. return false;
  22. }
  23.  
  24. var cost = baseFare + (costPerMile * milesTravelled);
  25. // add the nightSurcharge to the cost if it is after
  26. // 8pm or before 6am
  27. if (pickupTime >= 21 || pickupTime < 6) {
  28. cost += nightSurcharge;
  29. }
  30. document.getElementById("result").innerHTML = "Your taxi fare is: $. "  + cost.toFixed(2);
  31. }
  32.  
And here is the form
Expand|Select|Wrap|Line Numbers
  1. <form>
  2. Miles for Journey <input type="text" id = "miles" required><br>
  3. Pickup Time <input type = text id = "putime" required><br><br>
  4. <input type="button" value="Calculate Fare" onclick="TaxiFare()">
  5. <input type="reset" value="Clear"><br><br>
  6. <span id = "result"></span>
  7. </form>
  8.  
Apr 13 '12 #1
Share this Question
Share on Google+
11 Replies


P: 4
Expand|Select|Wrap|Line Numbers
  1. if(Textbox1.Text!="")
  2. {
  3. MessageBox.Show("Please Enter The Values");
  4. }
Apr 13 '12 #2

P: 6
but i need to do it in javascript
Apr 13 '12 #3

P: 4
Expand|Select|Wrap|Line Numbers
  1. if(document.getelementbyid(textbox id).value!="")
  2. {
  3. alert("Please Enter The Values");
  4. }
  5.  
If This The Corrrect Answer Plz Post Your Result
Apr 13 '12 #4

P: 6
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"
Apr 13 '12 #5

P: 4
Dont Put the Text id Word ,Instead of give the TextBox id is there in ASP .net give that one.............
Apr 13 '12 #6

P: 6
sorry but i don't understand what u said.
Apr 13 '12 #7

P: 4
Give the TextBox id Name...........
Apr 13 '12 #8

P: 6
no it isn't validating the time field.
Apr 13 '12 #9

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 13 '12 #10

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 13 '12 #11

P: 6
ok i got it by adding (pickupTime == "") to line 27. Thank you acoder!
Apr 13 '12 #12

Post your reply

Sign in to post your reply or Sign up for a free account.